I want to perform a generic insert in entity framework. This is what I wrote -
static public void Insert<T>(MyEntities DataContext, T obj) where T : class
{
try
{
DataContext.AddObject(DataContext,obj);
DataContext.SaveChanges();
}
catch (Exception e)
{
throw new Exception("Problems adding object" + e);
}
}
But as you can see the AddObject method is not what i want...it gives exception as it expects an enitysetname I want to pass in the object and then add that object to my database. But I cannot do AddtoObjectName() as I dont know the object. Can anyone point me in the right direction here..
In EF 4 you can do:
var os = DataContext.CreateObjectSet<T>();
os.AddObject(obj);
DataContext.SaveChanges();
And please remove the stack-eating try/catch.
The problem is that the Entity Framework allows for the possibility of multiple sets that use the same type, so it must have a type name in order to work. If you know that you won't be using multiple sets with the same type, you can follow a naming convention that makes it possible to construct the name from the type.
We had the same issue, and we originally decided to name each entity set [Type]Set (e.g. FormSet, ActivitySet).
With the advent of .NET 4.0, Microsoft has exposed the API they use for pluralizing entity sets in the EF tool in Visual Studio, so we're looking at maybe sticking with the default plurals, and using that tool to figure out what the default name is (e.g. Forms, Activities).
using System.Data.Entity.Design.PluralizationServices;
...
internal static readonly PluralizationService PluralizationService =
PluralizationService.CreateService(CultureInfo.CurrentCulture);
...
static public void Insert<T>(MyEntities DataContext, T obj) where T : class
{
try
{
string setName = PluralizationService.Pluralize(typeof(T).Name);
DataContext.AddObject(setName,obj);
DataContext.SaveChanges();
}
catch (Exception e)
{
throw new Exception("Problems adding object" + e);
}
}
Related
I have an entity framework context with tables EntityTypeA, EntityTypeB ... EntityTypeZ. I would like to create a method which returns an IEnumerable of IEntityModel, or in other words the content of the tables listed above.
I currently have a switch which, based on the type provided as argument, returns the content of the corresponding table.
Please consider the following code that I'm trying to factorize:
IEnumerable<IEntityModel> GetAllEntitiesByType(Type entityType)
{
NorthwindEntities en = new NorthwindEntities();
switch (entityType.Name)
{
case "EntitiesTypeA":
return en.EntitiesTypeA;
// all types in between
case "EntitiesTypeZ":
return en.EntitiesTypeZ;
default:
throw new ArgumentException("Unknown model type: " + entityType);
}
}
I would be surprised if there were no other more concise way to achieve the same result (by using reflection for instance) but I can't seem to find a useful example.
Any ideas please?
You can use the non generic DbContext.Set method to get the corresponding DbSet and then cast it to IEnumerable<IEntityModel> (important - do not use Cast method but regular C# cast operator):
IEnumerable<IEntityModel> GetAllEntitiesByType(Type entityType)
{
NorthwindEntities en = new NorthwindEntities();
return (IEnumerable<IEntityModel>)en.Set(entityType);
}
Consider using generic repository pattern. Here you can find an example.
Then implement the GetAllEntitiesByType function in your repository like this:
IEnumerable<T> GetAllEntitiesByType()
{
return entities.Set<T>();
}
So, the question at hand is about initializing the lazy collections of an "unknown" entity, as long as these are known at least by name. This is part of a more wide effort of mine to build a generic DataTable -> RecordDetails miniframework in JSF + Primefaces.
So, the associations are usually lazy, and the only moment i need them loaded is when someone accesses one record of the many in the datatable in order to view/edit it. The issues here is that the controllers are generic, and for this I also use just one service class backing the whole LazyLoading for the datatable and loading/saving the record from the details section.
What I have with come so far is the following piece of code:
public <T> T loadWithDetails(T record, String... associationsToInitialize) {
final PersistenceUnitUtil pu = em.getEntityManagerFactory().getPersistenceUnitUtil();
record = (T) em.find(record.getClass(), pu.getIdentifier(record));
for (String association : associationsToInitialize) {
try {
if (!pu.isLoaded(record, association)) {
loadAssociation(record, association);
}
} catch (..... non significant) {
e.printStackTrace(); // Nothing else to do
}
}
return record;
}
private <T> void loadAssociation(T record, String associationName) throws IntrospectionException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
BeanInfo info = Introspector.getBeanInfo(record.getClass(), Object.class);
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (PropertyDescriptor pd : props) {
if (pd.getName().equals(associationName)) {
Method getter = pd.getReadMethod();
((Collection) getter.invoke(record)).size();
}
}
throw new NoSuchFieldException(associationName);
}
And the question is, did anyone start any similar endeavor, or does anyone know of a more pleasant way to initialize collections in a JPA way (not Hibernate / Eclipselink specific) without involving reflection?
Another alternative I could think of is forcing all entities to implement some interface with
Object getId();
void loadAssociations();
but I don't like the idea of forcing my pojos to implement some interface just for this.
With the reflection solution you would suffer the N+1 effect detailed here: Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans
You could use the OpenSessionInView instead, you will be affected by the N+1 but you will not need to use reflection. If you use this pattern your transaction will remain opened until the end of the transaction and all the LAZY relationships will be loaded without a problem.
For this pattern you will need to do a WebFilter that will open and close the transaction.
How can I insert data into my database using Entity Framework ?
I'm trying this code bellow, but, in the method AddObject I have to pass 2 parameter:
string entitySetName
object entity
What is the entitySetName ?
The object entity is my model ?
EntityNetimoveis.San2011Entities db = new EntityNetimoveis.San2011Entities();
Model.Gerenciar.UsuarioCurso us = new Model.Gerenciar.UsuarioCurso();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
try
{
us.Usuario_Id = Convert.ToInt32(context.Request["id"]);
db.San_UsuarioCurso.Context.AddObject(us);
db.SaveChanges();
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("");
}
}
It's the name of your ObjectSet, in your case San_UsuarioCurso.
You're going back and forth in your references however.
In db.San_UsuarioCurso.Context. You're referencing your context (db) and then going to your objectset (San_UsuarioCurso) and then going back to your context (Context).
You can add directly to the object set like this, and not have to define the name of the entity set:
db.San_UsuarioCurso.AddObject(us);
Or if you want to use your function this should work as well:
db.San_UsuarioCurso.Context.AddObject("San_UsuarioCurso", us)
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem:
created an EF Model (no POCOs), generic repository on top of it and a RIA Service(hosted in an ASP.NET MVC application) and tried to get data from within the ASP.NET MVC application: worked well.
Next step: Silverlight client. Got a reference to the RIAService (through its context), queried for all the records of the repository and got them into the SL application as well (using this code sample):
private ObservableCollection<Culture> _cultures = new ObservableCollection<Culture>();
public ObservableCollection<Culture> cultures
{
get { return _cultures; }
set
{
_cultures = value;
RaisePropertyChanged("cultures");
}
}
....
//Get cultures
EntityQuery<Culture> queryCultures = from cu in dsCtxt.GetAllCulturesQuery()
select cu;
loCultures = dsCtxt.Load(queryCultures);
loCultures.Completed += new EventHandler(lo_Completed);
....
void loAnyCulture_Completed(object sender, EventArgs e)
{
ObservableCollection<Culture> temp=
new ObservableCollection<Culture>loAnyCulture.Entities);
AnyCulture = temp[0];
}
The problem is this: whenever i try to edit some data of a record (in this example the first record) i get this error:
This EntitySet of type 'Culture' does not support the 'Edit' operation.
I thought that i did something weird and tried to create an object of type Culture and assign a value to it: it worked well!
What am i missing? Do i have to declare an EntitySet? Do i have to mark it? Do i have to...what?
Thanks in advance
It turns out that in the DomainService class one has to implement (or at least to mark "placeholder methods") as "Edit", "Delete",... eg
[Delete]
public void DeleteCulture(Culture currentCulture)
{
throw new NotImplementedException("UpdateCulture not Implemented yet");
}
[Insert]
public void InsertCulture(Culture newCulture)
{
throw new NotImplementedException("InsertCulture not Implemented yet");
}
This way the OrganizationDomainContextEntityContainer class creates an EntitySet with parameter EntitySetOperations.All (meaning that all the CUD operations are available).
Hope it's useful for someone in the future!
I have a model in Ado.Net EF.
I have a one to many relation and when I want to Add the entities I get the error
"An entity object cannot be referenceed by multiple instances of IEntityChangeTracker"
Any clue?
Something similar to
Template template = new Template();
...
...
while (from < to)
{
Course course = new Course();
.....
template.Course.Add(course);
.....
}
courseEntities.AddToTemplate(template); // Problem line
courseEntities.SaveChanges();
I was getting this message until i started to store the data context in the HttpContext.Items Property. This means you can use the same data context for the current web request. That way you don't end up with 2 data contexts referencing the same entities.
Here is a good post on DataContext Life Management.
I hope it helps.
Dave
"template", or something that it references, has already been added to courseEntities or another context. I don't see anything in the code you show it would do that, but it is certainly happening. Perhaps it's happening in some of the code that you've trimmed. Look at the EntityState property of "template" in the debugger, and look at the EntityState of the properties of "template" as well. This should help you find out which entity instance is already in a context.
I already realize the problem. I have another relation and I get the other entity from another context.
Let me relate my experience with this nasty error and point out the terrain chasing it will take you over leading to a tremendously simple solution.
CompanyGroup is pretty simple. It has a name and it has a Company object.
I started with this:
1 public static void Add(CompanyGroup item)
2 {
3 try
4 {
5 using (Entities scope = new Entities())
6 {
7 scope.AddToCompanyGroup(item);
8 scope.SaveChanges();
9 }
10 }
11 catch (Exception ex)
12 {
13 LogException(ex, item);
14 throw;
15 }
16 }
And got this error:
{"An entity object cannot be
referenced by multiple instances of
IEntityChangeTracker."}
So, I added this between lines 6 and 7:
(IEntityWithChangeTracker)item).SetChangeTracker(null);
That rewarded me with:
{"The object cannot be added to the
ObjectStateManager because it already
has an EntityKey. Use
ObjectContext.Attach to attach an
object that has an existing key."}
So I changed
scope.AddToCompanyGroup(item);
to
scope.Attach(item);
Now it complained about:
{"An object with a temporary EntityKey
value cannot be attached to an object
context."}
(beginning to sound like some of the girls I dated in my youth -- never content -- but I digress)
So I made the entity key null (didn't work) and used the method to create new (didn't work, either)
Along the way, I got this error, too:
{"The source query for this
EntityCollection or EntityReference
cannot be returned when the related
object is in either an added state or
a detached state and was not
originally retrieved using the
NoTracking merge option."}
The Solution?
Replace the core, lines 7 and 8, with:
CompanyGroup newcg = new CompanyGroup();
newcg.GroupName = item.GroupName;
newcg.Company = scope.Company.Where(c => c.CompanyID == item.Company.CompanyID).First();
scope.AddToCompanyGroup(newcg);
scope.SaveChanges();
Essentially, I took the data passed via 'item', and moved it to newly created object of the same type that introduces the same
scope as the one used in the Add.
I hope this is the simplest and correct solution. You need one db context per httprequest.
EF4 Code First template Global.asax.cs
http://gist.github.com/574505
void MvcApplication_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Items[SessionKey] = new Db();
}
void MvcApplication_EndRequest(object sender, EventArgs e)
{
var disposable = HttpContext.Current.Items[SessionKey] as IDisposable;
if (disposable != null)
disposable.Dispose();
}
Please initialize your Entities only one time.
Like as
If You more than one time initialize your Entities.
You will get the error:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
ex:
public class Test
{
private Entities db=new Entities();
public static void Add(CompanyGroup item)
{
try
{
db.CompanyGroup.Add(item);
db.SaveChanges();
}
catch (Exception ex)
{
}
}
}
This problem I was solved by removing from the object that I update, extra relationships with other entities (Virtual). Left only their ID.
This is wrong code
dataFileEntity.IterParameterValue = parameterValueEntity.ParameterValue;
dataFileEntity.IterParameterValueId = parameterValueEntity.ParameterValue.Id;
dataFileEntity.ResultParameter = parameterValueEntity.ResultParameter;
dataFileEntity.ResultParameterId = parameterValueEntity.ResultParameter.Id;
dataFileEntity.RawDataResult = result.Value;
This is right
dataFileEntity.IterParameterValueId = parameterValueEntity.ParameterValue.Id;
dataFileEntity.ResultParameterId = parameterValueEntity.ResultParameter.Id;
dataFileEntity.RawDataResult = result.Value;
RequestTestRawDataFileRepository.AddOrUpdate(dataFileEntity);
Я эту проблему решила, убрав из объекта, который я апдейтила лишние связи с другими сущностями (Virtual). Оставила только их id.