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 6075689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:31:22+00:00 2026-05-23T10:31:22+00:00

hey guys, i am working in a MVC2 project with EF4 and i am

  • 0

hey guys, i am working in a MVC2 project with EF4 and i am having the following exception:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

i am trying to do something like this:

Transaction transaction = new Transaction();
transaction.Amount = response.Amount;
...
_transactionService.Add(transaction);
_transactionService.Save();

OrderPayment orderPayment = new OrderPayment();
orderPayment.AuthorizationTransaction = transaction;
...
_orderPaymentService.AddOrderPayment(orderPayment);
_orderPaymentService.Save();

i have 3 layers

  1. A repository layer where i have all the EF4 logic, and the basic CRUD for each entity
  2. A service layer that apply all my business logic and uses the repo, of course, i dont have in here any reference to the objectcontext of the EF4
  3. And the MVC stuff in my web layer

the above code belongs to a controller of the web layer, and my repos are:

OrderPaymentRepo

public void AddOrderPayment(OrderPayment orderPayment)
{
    _pharmacyDpnCtx.OrderPayments.AddObject(orderPayment);
}

public int Save()
{
    return _pharmacyDpnCtx.SaveChanges();
}

and the TransactionRepo:

public void Add(Transaction transaction)
{
    _pharmacyDpnCtx.Transactions.AddObject(transaction);
}

public int Save()
{
    return _pharmacyDpnCtx.SaveChanges();
}

i was researching in the web but a lot of solutions has the UnityOfWork to use the same ObjectContext, that is the best solution but now i can afford that, any suggestion without UnityOfWork

  • 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-23T10:31:22+00:00Added an answer on May 23, 2026 at 10:31 am

    First, take a look at this link – 8 Entity Framework Gotchas, at the 5th and 6th chapter, there’s a full explanation about your question.

    Second, the easy thing you can do is to use the same “Context” object in the Save and Add method.

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

Sidebar

Related Questions

hey i guys i want help from you. i am working on website project,
Hey guys, working on an event calendar. I'm having some trouble getting my column
Hey Guys...need help. Working on a project and get this error on the Output
Hey guys i'm working on admin module for my project. When a person logs-in,
Hey guys I'm now working on a project that require upload a lot of
Hey guys I am working on a project where I need to broadcast a
Hey guys I'm working on a project in C# were I must replace any
Hey Guys I am currently working on a project which will access the accelerometer
Hey guys I have the following code working which is sending a GET Request
Hey guys i am having an issue with an assignment I am working on.

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.