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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:41:39+00:00 2026-06-14T00:41:39+00:00

I cannot get my relationship to save correctly on update/create with Entity Framework 5.

  • 0

I cannot get my relationship to save correctly on update/create with Entity Framework 5.

I’ve got a new MVC 4.0 project with EF 5 and I’m using code-first migrations.

My models look like this.

public class Blog
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Post
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Text{ get; set; }
    public virtual Blog Blog { get; set; }
}

My migration correctly creates a Blog_Id column in my posts table.

Post Controller:

var db = new AppContext();
ViewBag.Blogs = new SelectList(db.Blogs.ToList(), "ID", "Title", post.Blog_Id);

Post View:

<p>
    @Html.LabelFor(model => model.Blog.Id, "Blog")
    @Html.DropDownListFor(model => model.Blog.Id,
         (SelectList)ViewBag.Blogs, "Select blog", new { })
    @Html.ValidationMessageFor(model => model.Blog.Id)
</p>

When I create/update the post though, the model has the correct relationship before saving, but the Blog_Id column does not persist to the database.

My update function looks like this.

var db = new AppContext();
db.Posts.Attach(post);
db.Entry(post).State = EntityState.Modified;
db.SaveChanges();

Any help would be greatly appreciated. All of the documentation and examples I’ve looked at seems to be outdated (mostly uses EF4) and the MSDN docs seem to be self referential (EF say to look at docs, docs say to look at EF site) and difficult to navigate.

  • 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-14T00:41:41+00:00Added an answer on June 14, 2026 at 12:41 am

    The simplest solution is to expose the foreign key as a property in your model:

    public class Post
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Text{ get; set; }
    
        public int BlogId { get; set; } // FK property
        public virtual Blog Blog { get; set; }
    }
    

    Then bind your post view to that property:

    <p>
        @Html.LabelFor(model => model.BlogId, "Blog")
        @Html.DropDownListFor(model => model.BlogId,
            (SelectList)ViewBag.Blogs, "Select blog", new { })
        @Html.ValidationMessageFor(model => model.BlogId)
    </p>
    

    If you don’t want that you need change tracking to make EF recognize that the relationship has changed:

    var db = new AppContext();
    var blog = post.Blog;
    post.Blog = null;
    
    db.Posts.Attach(post);
    db.Entry(post).State = EntityState.Modified;
    post.Blog = blog; // EF now recognizes a change of the relationship
    
    db.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When creating many to many relationship using EF 4.3 code first approach, I cannot
I am working on a new project that makes use of Entity Framework. I
I'm working with Entity Framework code first. The error occurs when trying to save
I have a one-to-many relationship and cannot get cascade to work, once I set
Doing Entity Framework (5) code first, although we are not generating our data model
I'm using Entity Framework 4.3 with a simple design: public class Post { [Key]
I am trying to update an entity with a FK relationship in EntityFramework 4.3
I have the following code in an aspx file that I cannot get to
This seems like the most common relationship but for some reason I cannot get
Hello I am a Entity Framework newb and I am trying to get 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.