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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:43:30+00:00 2026-06-04T00:43:30+00:00

I think I have read every article and stack overflow question regarding this, but

  • 0

I think I have read every article and stack overflow question regarding this, but cannot work out the solution. Let me start out with my models

public class Entry
{
    public Entry ()
    {
        DateEntered = DateTime.Now;
    }

    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string Email { get; set; }
    public string Number { get; set; }
    public string FbId { get; set; }
    [ReadOnly(true)]
    public DateTime DateEntered { get; set; }
    public string AccessToken { get; set; }

    //Relationsips
    public Backgrounds Background { get; set; }
    public Cars Car { get; set; }
}

public class Backgrounds
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Filename { get; set; }
}

public class Cars
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string FileName { get; set; }
}

Now in my controller, I am updating the entry. Like follows

    // PUT /api/entries/5
    public HttpResponseMessage Put(Entry entry)
    {
        if(ModelState.IsValid)
        {

            _db.Entries.Attach(entry);
            _db.Entry(entry).State = EntityState.Modified;
            _db.SaveChanges();

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }

        throw new HttpResponseException(HttpStatusCode.BadRequest);
    }

My Entry model gets updated correctly, but if for eg entry.Background.Name changes, this will not be persisted to the database. My controller is accepting the entire entry model including its relationships => Backgrounds and Cars. However any value that is changed to the relationship is not updated or reflected. Any elegant solution without having to query the database then updating? I dont want to have any extra queries or lookups before I update.

Thanks

Tyrone

  • 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-04T00:43:32+00:00Added an answer on June 4, 2026 at 12:43 am

    You must manually tell EF about all changes done to the object graph. You told EF just about change to entry instance but you didn’t tell it about any change to related entities or relations itself. There is no elegant way to solve this. You have generally two options:

    • You will use some DTOs instead your entities and these DTOs will have some flag like IsDirty – when you receive object graph back to your controller you will reconstruct entities from DTOs and set their state based on IsDirty. This solution needs further extensions for example if your client can also delete relations.
    • You will query object graph from database and merge your incoming changes to entities retrieved from database.

    There are some partial solutions like forcing to save changes to all related objects by setting their state to modified and identifying new objects by Id == 0 but again these solutions work only in specific scenarios.

    More complex discussion about this problem.

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

Sidebar

Related Questions

I think I might have read every usort article on StackOverflow, but I can't
i have read some topics on this, but i don't quite think it answers
I think I have this working but I need advice. I'd like to know
I've spent the last 2 hours on this and I've probably read every question
I've read every response I could fine on SO before posting this question. Although
I have read other questions on this topic but none that actually answers my
I think I read somewhere that some modules only have object oriented interfaces (
I think I have a basic understanding of REST, but something I'm stuck on
I think I have a pretty simple goal but cant seem to reach it.
I have read in many posts that global variables are bad, but I need

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.