Refresh entity instance with DbContext

旧巷老猫 提交于 2019-11-26 18:04:26

问题


With EF4 CTP5 DbContext, what is the equivalent of this

    public void Refresh(Document instance)
    {
        _ctx.Refresh(RefreshMode.StoreWins, instance);
    }

I've tried this but it doesn't do the same thing, updating the instance

    public void Refresh(Document instance)
    {
        _ctx.ChangeTracker.DetectChanges();
    }

?


回答1:


You must use this:

public void Refresh(Document instance)
{
  _ctx.Entry<Document>(instance).Reload();
}



回答2:


The above doesn't work. Reload() method does not correctly refresh the entity from the database. It performs SQL select query but does not build proxies for the navigational properties. See the example below (I use the Northwind database in SQL Server with EF 5.1):

NorthwindEntities northwindEntities = new NorthwindEntities();
Product newProduct = new Product
{
    ProductName = "new product",
    Discontinued = false,
    CategoryID = 3
};
northwindEntities.Products.Add(newProduct);
northwindEntities.SaveChanges();

// Now the product is stored in the database. Let's print its category

Console.WriteLine(newProduct.Category); // prints "null" -> navigational property not loaded

// Find the product by primary key --> returns the same object (unmodified)
// Still prints "null" (due to caching and identity resolution)
var productByPK = northwindEntities.Products.Find(newProduct.ProductID);
Console.WriteLine(productByPK.Category); // null (due to caching)

// Reloading the entity from the database doesn't help!
northwindEntities.Entry<Product>(newProduct).Reload();
Console.WriteLine(newProduct.Category); // null (reload doesn't help)

// Detach the object from the context
((IObjectContextAdapter)northwindEntities).ObjectContext.Detach(newProduct);

// Now find the product by primary key (detached entities are not cached)
var detachedProductByPK = northwindEntities.Products.Find(newProduct.ProductID);
Console.WriteLine(detachedProductByPK.Category); // works (no caching)

I may conclude that real refresh / reload of EF entity can be done by Detach + Find:

((IObjectContextAdapter)context).ObjectContext.Detach(entity);
entity = context.<SomeEntitySet>.Find(entity.PrimaryKey);

Nakov




回答3:


I found that the reload fails on proxy entities that have navigation properties.

As a work around, reset the current values and then reload like this:

var entry =_ctx.Entry<Document>(instance);
entry.CurrentValues.SetValues(entry.OriginalValues); 
entry.Reload();


来源:https://stackoverflow.com/questions/5221314/refresh-entity-instance-with-dbcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!