0

I've built a site in MVC3 using EF 4.0 using the Repository pattern. Everything was going good but I'm starting to run into a lot of "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects" errors. It seems that my repository layer is getting the contexts all mixed up, so I figured it might just be easier to start a new EF4.1 project.

At first I looked into Repository Pattern + Unit of Work, but came across some threads saying that this isn't needed for EF4.1. I came across this thread saying "DbContext is implementation of unit of work pattern and IDbSet is implementation of repository pattern.". I figured maybe then I could just use that. Upon further inspection though it seems that DbContext uses the Code First approach, which as far as I can tell will drop and create the database again if the POCOs change. I need to keep the data in my database, so as far as I can tell that option is out.

My head is spinning right now with EF options. Is the Repository pattern needed with EF4.1? Is DbContext meant for working on databases that are already full of data? Is there a better way of managing the entity contexts that don't involve these?

Any push in the right direction would be great =/

Community
  • 1
  • 1
boolean
  • 891
  • 2
  • 14
  • 23

1 Answers1

1

A few comments. For details I recommend to do some basic research using a search engine.

...I'm starting to run into a lot of "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects" errors. It seems that my repository layer is getting the contexts all mixed up, so I figured it might just be easier to start a new EF4.1 project.

If you have this error you did something wrong. EF 4.1 won't protect you to do the same mistake again because you also cannot change relationships between objects that are attached to different DbContexts. You just have to analyze and debug your code and find the source of the problem.

...this thread saying "DbContext is implementation of unit of work pattern and IDbSet is implementation of repository pattern.". I figured maybe then I could just use that...

ObjectContext and ObjectSet<T> is an implementation of those patterns as well. This is no reason to change the version of Entity Framework.

Upon further inspection though it seems that DbContext uses the Code First approach...

You can also use Database First and Model First approach with DbContext.

...which as far as I can tell will drop and create the database again if the POCOs change. I need to keep the data in my database, so as far as I can tell that option is out.

You can turn that feature off. Also, EF 4.3 has a migration feature which helps to update and evolve an existing database schema.

Is the Repository pattern needed with EF4.1?

No. It's also not needed for ObjectContext. To be precise, it's not needed that you write your own (abstract) repository of top of EF, because EF is already an implementation of that pattern.

Is DbContext meant for working on databases that are already full of data?

Yes. The additional feature to create a database from code (Code-First) is mainly a productivity tool for the development phase of your application which is supposed to be disabled in production.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • Interesting. Seems like 4.1 will just give me the same issues so I might be best to, as you said, just fix the current issue. Lots of good pointers to use though, cheers! – boolean Mar 24 '12 at 23:04
  • @boolean: BTW, current version `DbContext`/Code First is EF 4.3.1. If you think about upgrading, step directly to that version. – Slauma Mar 24 '12 at 23:08