After setting EntityState.Unchanged the entity properties are lost

asp.net-mvc c# entity-framework entity-framework-6

Question

I've added a View a student entity to my Entity Framework 6 repository in an ASP.Net MVC project.

class Student
  public long Id { get; set; }
  public virtual Book Favorite { get; set; }

class Book
  public long Id { get; set; }
  public string Title { get; set; }

The Favorite A drop-down menu in the view is used to set a property. just the on-post controller in theId the novel is based on. I set the EntityState for the Book to Unchanged as per the description in here to instruct Entity Framework to only add the student and not the linked book.

After saving my changes, the database now contains the correct student record and the book record is still in place. However, whenever I query the book directly from my repository (not through the student), I now receive a book entity with only the Id set and the Title and all other properties empty.

My Controller in a nutshell:

public ActionResult Create()
{
    StudentViewModel result = new StudentViewModel();
    result.BookList = new SelectList(DbContext.Books, "Id", "Name");
    return View(result);
}

[HttpPost]
public ActionResult Create(StudentViewModel viewModel)
{
    DbContext.Entry(viewModel.NewStudent.Favorite).State = EntityState.UnChanged);
    DbContext.Add(viewModel.NewStudent);
    DbContext.SaveChanges();

    Book missingProperties = 
        DbContext.Books.Single(book => book.Id == viewModel.NewStudent.Favorite.Id);
} 

My Opinion, in Brief:

@using System.Linq
@model StudentViewModel
@using (Html.BeginForm())
{        
    ...
    @Html.TextBoxFor(model => model.NewStudent.Name)
    @Html.DropDownListFor(model => model.NewStudent.Favorite.Id, Model.BookList)
    ...
}
1
0
5/23/2017 12:19:17 PM

Accepted Answer

The Entity Framework DbContext caches entities, which is a concern.

With lazy loading, you simply need to make some reference to the related data and the Entity Framework will check to see whether it’s been loaded into memory. If not, the Entity Framework will create and execute a query behind the scenes, populating the related data.

By default, Visual Studio will define newly created models to set LazyLoadingEnabled to true.

(from 11 to zzz)

Once loaded from the database, it won't be loaded again for the duration of the DbContext.EntityState.UnChanged stops the entity from being saved to the database by Entity Framework alone. However, whenever the entity is fetched from context a second time, the cached changed value rather than the one from the database is used.

Additionally, the modified entity needs to be refreshed. Reload it after attaching it to the DbContext.

Book result = Attach(book);
Entry(book).Reload();

Although I believe this resolves the matter, I'm not sure if GuruStron's solution makes advantage of the superior design.

1
2/12/2015 5:05:08 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow