5

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 Answer 1

9

...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>.

Sign up to request clarification or add additional context in comments.

2 Comments

what is the difference between dbcontext and objectcontext?
@stats101: DbContext is a wrapper around ObjectContext with a simplified API. It also contains the "Code First" development strategy. It is recommended to prefer DbContext because it is easier to use and work with - unless you have some advanced requirements which are not available with DbContext like mapping to Stored Procedures. Links: stackoverflow.com/a/3473323/270591 and blogs.msdn.com/b/adonet/archive/2011/04/11/ef-4-1-released.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.