我是EF的新手,我从EF5开始 .
遵循Performance Considerations for Entity Framework 5的建议,2.4.2将模型移动到单独的程序集中...我创建了一个单独的项目(我将其称为EfPrj)来管理我的Db上下文(称为MyDbContext) .
在我的域层(我将其称为DomainPrj)中,我使用了EfPrj中的实体 . 问题是在DomainPrj中我看不到继承自DbContext的MyDbContext成员 .
例如,如果我想更新表用户,则代码为:
void UpdateUser(User u)
{
MyDbContext db = new MyDbContext();
//whatever is needed
db.SaveChanges();
}
但是在EfPrj中它没有问题 . 在DomainPrj中,我看不到成员函数SaveChanges(),收到此错误:
'MyDbContext' does not contain a definition for 'SaveChanges' and no extension method 'SaveChanges' ... could be found (are you missing a using directive or an assembly reference?)
Update: EfPrj仅是具有ORM数据库优先模型的项目 . MyDbContext默认定义为EF5对象:public partial class MyDbContext:DbContext {public MyDbContext():base("name=MyDbEntities"){}
所以派生自DbContext并且是公开的 . DomainPrj引用EfPrj和 MyDbContext db = new MyDbContext()
工作没有问题 .
2 回答
您是否尝试过从DomainPrj引用EntityFramework?
由于这些方法是在DbContext中定义的,而DbContext是在EntityFramework中定义的,因此必须引用它 .
通常,如果您不仅要使用类,还要使用在程序集中定义的委托,方法,属性等;你必须有一个对该程序集的引用 .
对我来说,我不得不添加Microsoft.Data.Services.Client.dll