4

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.

4
  • Does MyDbContext derive from DbContext? Jan 14, 2013 at 15:50
  • Have your referenced your EfPrj in you DomainPrj and is your DbContext a public member
    – Derek
    Jan 14, 2013 at 15:50
  • Another obvious one: do you get a compiler error about referencing the EntityFramework assembly? Jan 14, 2013 at 15:51
  • just copy the ConnectionString from your seperated project and paste to your main project
    – spajce
    Jan 14, 2013 at 16:39

2 Answers 2

12

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.

1
  • DomainPrj references EfPrj and MyDbContext db = new MyDbContext() works without problems.
    – Alex
    Jan 14, 2013 at 18:39
0

For me I had to add Microsoft.Data.Services.Client.dll

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.