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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:45:39+00:00 2026-06-11T06:45:39+00:00

I had a question more detailed earlier which I had no answer, I will

  • 0

I had a question more detailed earlier which I had no answer, I will have the same question with a simpler way:

I have an EF database with foreign key to another table.
enter image description here

I would like to UPDATE an ENTITY. But I need to this like this and I’ll write the codes below:

  1. Go to database and retrieve the Member by id, return EF Member object
  2. Do some changes on the object OUTSIDE the EF Context
  3. Send the MODIFED EF Member into a Save method
  4. In BL layer save method uses the context and save changes.

1)

MemberManager currentMemberManager = new MemberManager();
Member NewMember = currentMemberManager.GetById(2);

2)

NewMember.FirstName = "NewFirstName";
NewMember.LanguageId = 1;

3)

currentMemberManager.Save(NewMember);

4)

public void Save2(Member newMember)
{
    using (var Context = new NoxonEntities())
    {
        Member existingMember = Context.Member.First(c => c.Id == newMember.Id);
        existingMember.FirstName = newMember.FirstName;
        existingMember.Language = Context.Language.First(c => c.Id == newMember.LanguageId);
        Context.SaveChanges();//In here I get the error below
    }
}

The changes to the database were committed successfully, but an error
occurred while updating the object context. The ObjectContext might be
in an inconsistent state. Inner exception message: A referential
integrity constraint violation occurred: The property values that
define the referential constraints are not consistent between
principal and dependent objects in the relationship.

Note: You may suggest to SEND a different class (Ex: public class
MyMember) that has all the necessary properties and totally separated
from EF. But this requires much work to get all EF object converting
into my separate classes. Am I right?

I am hoping there is a way to Detach the entity just long enough for me to modify it and save the values into database. (Also, I tried the Detach method which updates no rows at all)

I’ve been trying to solve this for hours now.

Please, help me to understand it better, I really need a solution. Thank you so much to anyone how has some ideas.

  • 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-06-11T06:45:41+00:00Added an answer on June 11, 2026 at 6:45 am

    Could you do something simple like detaching the entity, then attaching it to the context when you’re ready to save?

    MemberManager currentMemberManager = new MemberManager();
    Member NewMember = currentMemberManager.GetById(2);
    

    The get:

    public Member GetById(int id)
    { 
      var member = YourContext.Members.FirstOrDefault(m => m.id == id);
      YourContext.Detach(member);
      return member;
    }
    

    The save:

    public void Save2(Member newMember)
            {
                using (var Context = new NoxonEntities())
                {
                    Context.Attach(newMember);
                    Context.ObjectStateManager.ChangeObjectState(newMember, EntityState.Modified);
                    Context.SaveChanges();
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday I had question (answered), which I have to expand a bit more today.
I have a faceted plot (about which I had this other question). I would
I had asked this question Adding more attributes to LINQ to SQL entity Now,
I had a question about Java Class Path variables. If I have multiple jars
I had a question answered which raised another one, why following does not work?
Had this question in the interview yesterday. Which is better to use? Infix(with parenthesis)
I had a long detailed question about how to get a specific calendar's event
I had a question about C++ destructor behavior, more out of curiosity than anything
This is a more direct question stemming from an earlier more general question i
I had this question about arrays in JavaScript. I couldn't really find an answer.

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.