Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 758
Alex Avatar asked Dec 15 '22 15:12

Alex


1 Answers

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.

like image 74
Mehmet Ataş Avatar answered Dec 21 '22 23:12

Mehmet Ataş