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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:00:33+00:00 2026-05-25T11:00:33+00:00

I thought the following task would be seemingly easy to code, but I have

  • 0

I thought the following task would be seemingly easy to code, but I have spent a lot of time and not yet figured out the right way to solve it. Here is the scenario

I have three Entities City, Locality and Donor. City has many localities and each Locality has two One to Many relationship with donor as given below.

public class City {
    public virtual List<Locality> Localities { get; set; }
}

public class Locality {
    public virtual List<Donor> DonorsInOfficeLocality { get; set; }
    public virtual List<Donor> DonorsInResidenceLocality { get; set; }
}

public class Donor : User {
    public virtual Locality OfficeLocality {get;set;}
    public virtual Locality ResidenceLocality { get; set; }
}

In my Donor edit module I receive (Donor Id, residenceLocalityId and officeLocalityId). The Donor entity is in detached state comming from presentation layer. This code does not work. The old relationships remain intact.

public void EditDonor(Donor donor, int residenceLocalityId, int officeLocalityId) {
    Locality residenceLocality = context.Localities.Find(residenceLocalityId);
    donor.ResidenceLocality = residenceLocality;
    Locality officeLocality = context.Localities.Find(officeLocalityId);
    donor.OfficeLocality = officeLocality;
    context.Donors.Attach(donor);
    context.Entry(donor).State = EntityState.Modified;
    context.SaveChanges();
}

One more thing I tried is changing the state of ResidenceLocality and OfficeLocality to Modified, but that didn’t work either. The old relations remained intact. And in this case, since I change the Locality as Modified, I have to additionally load City to avoid Update Exception for the modified locality.

If someone can let me know how to modify One to Many relations (as above) for entity in detached state received from presentation layer.

  • 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-25T11:00:34+00:00Added an answer on May 25, 2026 at 11:00 am

    you can keep both navigation property and the foreign key in your model.

    public class Locality {
         public int Id{get;set;}
        public virtual List<Donor> DonorsInOfficeLocality { get; set; }
        public virtual List<Donor> DonorsInResidenceLocality { get; set; }
    }
    
    
    public class Donor : User {
        public virtual Locality OfficeLocality {get;set;}
        public int OfficeLocalityId{get;set;}
    
        public virtual Locality ResidenceLocality { get; set; }
        public int ResidenceLocalityId{get;set;}
    }
    

    if you have the foreign key properties you don’t need to find by id to update navigation properties,

    public void EditDonor(Donor donor, int residenceLocalityId, int officeLocalityId) {
         donner.OfficeLocalityId=officeLocalityId;
         donor.ResidenceLocalityId=residenceLocalityId;
        context.Donors.Attach(donor);
        context.Entry(donor).State = EntityState.Modified;
        context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this task is easy, but I have no clue how to realize
I thought the following line of code should work fine: $(.1).attr('href', '#Home'); Right? But
Never thought I'd have this problem :) The following snippet of code works in
I'm not yet a skilled programmer but I thought this was an interesting problem
I thought this would be a simple task, but I'm currently unable to get
This is what I thought would be a simple select clause, however the following
What's the difference between the following two declarations? I thought they were equivalent, but
I have following code in my deploy.rb namespace :app do desc copies the configuration
I have a task which i need to cancel if the wait time is
We currently have the following task ahead, and I'm struggling to find what I

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.