简体   繁体   中英

EF5 model in a separate project can't see DbContext methods

I'm new to EF, and I'm starting with EF5.

Following the recommendations of Performance Considerations for Entity Framework 5 , 2.4.2 Moving your model to a separate assembly... I created a separated project (I will call it EfPrj) to manage my Db Context (called MyDbContext).

In my Domain layer (I will call it DomainPrj) I use the entities from the EfPrj. The problem is that inside the DomainPrj I can't see the members of MyDbContext that are inherited from DbContext.

For example, if I want to update a table Users the code is:

void UpdateUser(User u)
{
    MyDbContext db = new MyDbContext();
    //whatever is needed
    db.SaveChanges();
}

But in the EfPrj it works without problems. In the DomainPrj I can't see the the member function SaveChanges(), getting this error:

'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 es only a project with a ORM Database-First Model. MyDbContext is defined like EF5 object by default: public partial class MyDbContext : DbContext { public MyDbContext() : base("name=MyDbEntities") { }

So derives from DbContext and is public. DomainPrj references EfPrj and MyDbContext db = new MyDbContext() works without problems.

Have you tried referencing EntityFramework from your DomainPrj?

Since these methods are defined in DbContext and DbContext is defined in EntityFramework you must reference it.

In general, if you want to use not only classes but also delegates, methods, properties etc. that are defined in an assembly; you must have a reference to that assembly.

对我来说,我不得不添加Microsoft.Data.Services.Client.dll

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM