Entity Framework has issues introducing objects to databases.

.net asp.net-mvc c# db2 entity-framework

Question

I have the following code trying to add an object to the database:

public static void saveAudit(List<AUDIT> audit)
{
 Entities dao = new Entities();

 foreach (CMUAUDIT a in audit)
 {
    dao.CMUAUDITs.AddObject(a);
 }

 dao.SaveChanges();
}

However I get the error message:

"...does not contain a definition for 'AddObject' and no extension method 'AddObject' accepting a first argument of type 'System.Data.Entity.DbSet' could be found (are you missing a using directive or an assembly reference?)"

I've done some searching, and there is mention of the primary key having something to do with it. Any suggestions?

I'm using a DB2 database if that makes any difference?

1
4
5/25/2012 12:17:09 PM

Accepted Answer

...System.Data.Entity.DbSet...: Apparently your class Entities is derived from DbContext and not ObjectContext. CMUAUDITs will be a DbSet<T> (and not an ObjectSet<T>) in this case. The correct method to add an entity to a DbSet<T> is:

dao.CMUAUDITs.Add(a);

AddObject is only available for an ObjectSet<T>.

7
5/25/2012 12:13:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow