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

  • Home
  • SEARCH
  • 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 6228893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:28:01+00:00 2026-05-24T09:28:01+00:00

I’m having a small problem with ASP.NET MVC and Entity Framework 4. I have

  • 0

I’m having a small problem with ASP.NET MVC and Entity Framework 4. I have an entity called “UF” and another one called “Pais”, and they have this relation:

UF [* ... 0..1] Pais

I can access the Pais object directly from UF using a navigation property:

UF.Pais.Whatever = "foobar";

Currently I have a View which inserts a new item into the database, and it has an editor for “Pais.Codigo” (“Codigo” is the primary key for Pais). So when I insert a new UF, the framework creates an instance of class UF with a reference to an instance of class Pais. Then this is done:

if (ModelState.IsValid)
{
    db.UFs.AddObject(uf);
    db.SaveChanges();

    return View();
}

The problem is that the EF is inserting a new Pais into the database, so it basically ignores the existing one.

For example, if let’s say my object UF has a Pais with an ID of 1. The current value of uf.Pais.Codigo is 1. Other attributes, like the description, is currently null. When I execute the SaveChanges, both “uf” and “uf.Pais” are with the state of Added. The correct state for “uf.Pais” should be Unchanged, since it already exists on the database.

My question is: there’s some way of changing the default relationship EntityState for Unchanged? The following code solves the problem, but adding it to each function with adds a new entry to the database (and for each FK) is overkill!

db.ObjectStateManager.ChangeObjectState(uf.Pais, EntityState.Unchanged);

That’s it. I’m not sure if I was clear enough. Feel free to ask more information if needed. And sorry for any english mistakes!

Thanks,

Ricardo

PS: “Pais” stands for Country and “UF” for State.

  • 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-24T09:28:03+00:00Added an answer on May 24, 2026 at 9:28 am

    My question is: there’s some way of changing the default relationship
    EntityState for Unchanged?

    Yes by calling Attach instead of Unchanged.

    The following code solves the problem, but adding it to each function
    with adds a new entry to the database (and for each FK) is overkill!

    No it is not overkill, it is a solution because either Attach or AddObject will always make the operation for all entities and associations in entity graph. That means that calling AddObject will make everything what context is not aware of yet as Added and Attach will make everything what context is not aware of as Unchanged (so you will in turn have to set each modified or inserted entity to its correct state). That is how EF works if you are using detached entities.

    Another solution for the problem is making the connection after the UF is added:

    // Here UF.Pais is null
    db.UFs.AddObject(uf);
    // Create dummy Pais
    var pais = new Pais { Id = "Codigo" };
    // Make context aware of Pais
    db.Pais.Attach(pais);
    // Now make the relation
    uf.Pais = pais;
    db.SaveChanges();
    

    If you are working with detached entities you are always responsible for setting the correct state for each entity (and independent association). So you will either use attached entities to let EF make the magic for you (as shown in the example) or you will use the approach you dislike. In more complex scenarios you can find out that best approach is to load entity graph again and merge incoming changes into the attached graph.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.