Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:59:53+00:00 2026-05-31T18:59:53+00:00

I’ve built a site in MVC3 using EF 4.0 using the Repository pattern. Everything

  • 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 =/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:59:54+00:00Added an answer on May 31, 2026 at 6:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.