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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:53:50+00:00 2026-06-18T04:53:50+00:00

We are using EF5, Code First approach to an MVC4 app that we’re building.

  • 0

We are using EF5, Code First approach to an MVC4 app that we’re building. We are trying to update 1 property on an entity but keep getting errors. Here’s what the class looks like which the context created:

public partial class Room
{
    public Room()
    {
        this.Address = new HashSet<Address>();
    }

    public int RoomID { get; set; }
    public Nullable<int> AddressID { get; set; }
    public Nullable<int> ProductVersionID { get; set; }
    public string PhoneNumber { get; set; }
    public string AltPhone { get; set; }
    public string RoomName { get; set; }
    public string Description { get; set; }
    public string Comments { get; set; }
    public string Notes { get; set; }

    public virtual ICollection<Address> Address { get; set; }
}

Here’s our ViewModel for the view:

public class RoomDetailsViewModel
{
    //public int RoomID { get; set; }
    public string RoomName { get; set; }
    public string PhoneNumber { get; set; }
    public string AltPhone { get; set; }
    public string Notes { get; set; }
    public string StateCode { get; set; }
    public string CountryName { get; set; }
    public string ProductVersion { get; set; }
    public int PVersionID { get; set; }
    public List<SelectListItem> ProductVersions { get; set; }
    public Room Room { get; set; }
}

Here’s the Controller Action being called on “Save”:

    [HttpPost]
    public virtual ActionResult UpdateRoom(RoomDetailsViewModel model)
    {
        var db = new DBContext();

        bool b = ModelState.IsValid;

        var rooms = db.Rooms;
        var rm = rooms.Where(r => r.RoomID == model.Room.RoomID).Single();
        //List<Address> address = db.Addresses.Where(a => a.AddressID == rm.AddressID).ToList<Address>();

        rm.ProductVersionID = model.PVersionID;
        //rm.Address = address;

        db.Entry(rm).Property(r => r.ProductVersionID).IsModified = true;

        //db.Entry(rm).State = System.Data.EntityState.Modified;

        db.SaveChanges();

        return View("RoomSaved", model);
    }

All this view does is display data and allow the user to change the Product Version (from a SelectList), so, in the Room Entity, all we are updating is the ProductVersionID property, nothing else. We can get the data to display properly but when we click “save”, we get this error:

An object of type ‘System.Collections.Generic.List`1[[Models.Address,
Web.Mobile.TestSite, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]’ cannot be set or removed from the Value
property of an EntityReference of type ‘Models.Address’.

As you can see by the Controller Action, we’ve tried several different things but all seem to produce this error. I’ve tried to populate the model.Room.Address collection with an Address, without, but still get this error.

I read this StackOverflow article and this article as well but neither have solved my problem.

ANY help with this would be greatly appreciated!

  • 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-18T04:53:51+00:00Added an answer on June 18, 2026 at 4:53 am

    After hours and hours of digging, turns out that EF did not import some of the PK’s for my DB tables. What tipped me off to this was on the Room class, the PK RoomID did not have the [Key] attribute on it. I tried to reimport the table through the edmx but it never came through as a key (even though it’s clearly marked PK in the DB). So, to get around it, I created a partial class of my DBContext and override the OnModelCreating event and included the key, like so:

    public partial class DBContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Models.Room>().HasEntitySetName("Rooms");
            modelBuilder.Entity<Models.Room>().HasKey(r => r.RoomID);
        }
    }
    

    Once this was done, the Action saved the record as hoped.

    I hope this helps someone else!

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

Sidebar

Related Questions

I using manual mapping entity in my Code First on EF5 project and would
I am using EF5 code-first approach and I am just wondering should I detach
I am using Entity Framework 5 with Code First approach and using Fluent API
I am using Entity Framework (EF) 5.0, Code First approach, and SQL Server CE
I'm using Code-First DBContext-based EF5 setup. In DbMigrationsConfiguration.Seed I'm trying to fill DB with
I'm using EF5 Code First and I'm trying to store an IPAddress object. If
I'm building an MVC4 app using EF5 and ninject. Something broke when I upgraded
I'm using Code First with EF 5. Currently I have SPs that return just
I am using EF5 codefirst in an ASP.NET MVC4 app. I have products and
I'm using EF5 code first in a simple test application at the moment to

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.