English 中文(简体)
将对象添加到实体框架中数据库的问题
原标题:Problems adding object to database in Entity Framework

我有以下代码试图在数据库中添加一个对象:

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

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

 dao.SaveChanges();
}

然而,我得到错误信息:

"...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?)"

我进行了一些搜索,有人提到主要的钥匙与此有关。有什么建议吗?

我使用DB2数据库 如果有什么区别吗?

最佳回答

.System.Data.Entity.DbSet. :显然,在这种情况下,你的类别 Entities 来自 DbContext ,而不是 ObjectContext >CMUAUDITs 将是一个 DbSet<T> (而不是一个 ObjectSet<T> )。

dao.CMUAUDITs.Add(a);

AddObject 只能用于 ObjectSet<T>

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签