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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:32:18+00:00 2026-05-26T11:32:18+00:00

Following is a save routine on existing record. What’s wrong with this? I’m using

  • 0

Following is a save routine on existing record. What’s wrong with this? I’m using independent association

There’s no error emitted, however, the Country_CountryId field on Person table didn’t change, everything else are properly persisted. What’s wrong on the following code/approach?

public JsonResult SaveUpdate(Person p)
{
    p.Country = new Country { CountryId = new Guid("EF0CD98E-7138-4757-866E-ADC3C8D216DA") };

    using (var db = new TheDbContext())
    {
        db.Entry(p).State = System.Data.EntityState.Modified;                
        db.SaveChanges();       
    }

}

Here’s my mapper:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<Person>().HasRequired(x => x.Country)
        .WithMany(x => x.Persons)
        .Map(x => x.MapKey("Country_CountryId"));

    modelBuilder.Entity<Person>().Property(x => x.RowVersion).IsRowVersion();


    modelBuilder.Entity<Country>().HasKey(x => x.CountryId);

}

Here’s my models:

public class Country
{
    public virtual Guid CountryId { get; set; }

    public virtual string CountryCode { get; set; }
    public virtual string CountryName { get; set; }

    public virtual IList<Person> Persons { get; set; }
}


public class Person
{
    public virtual Guid PersonId { get; set; }
    public virtual string Username { get; set; }

    public virtual string Firstname { get; set; }
    public virtual string Lastname { get; set; }
    public virtual byte[] RowVersion { get; set; }


    public virtual Country Country { get; set; }
}
  • 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-26T11:32:18+00:00Added an answer on May 26, 2026 at 11:32 am

    Use this instead and it will work:

    public JsonResult SaveUpdate(Person p)
    {
        var country = new Country { CountryId = new Guid("EF0CD98E-7138-4757-866E-ADC3C8D216DA") };
    
        using (var db = new TheDbContext())
        {
            db.People.Attach(p);
            db.Countries.Attach(country);
            db.Entry(p).State = System.Data.EntityState.Modified;                
            p.Country = country;
            db.SaveChanges();       
        }
    }
    

    Independent associations require special care because each association itself has its own state which must be configured.

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

Sidebar

Related Questions

I have following data to save in database using php my data is :
I am using the following code to save an object and all its properties
is there anyway to save the following flash video locally, perhaps by streaming it
I am using the following code to save values from a settings view that
I am using the following generic code to save entities. using (ITransaction tx =
I am trying to save images using the following code: - (void)writeData{ if(cacheFileName==nil) return;
I am using the following code to save an image created on a Form
I'm using the following code to save a NSMutableDictionary to file then retrieve it
I am using following code to save screen-resolution in cookie var the_cookie=screen_resolution=+screen.width+x+screen.height+;expires=+today.setDate(today.getDate()+1); document.cookie=the_cookie; But
In my application I am using the following code to save images/files into the

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.