重新加载实体和所有导航属性关联 - DbSet 实体框架

Reload an entity and all Navigation Property Association- DbSet Entity Framework(重新加载实体和所有导航属性关联 - DbSet 实体框架)
本文介绍了重新加载实体和所有导航属性关联 - DbSet 实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a problem with entity association refresh. When I get an entity with like this:

MyContext context = new MyContext();

Person myPerson = context.PersonSet.FirstOrDefault();
String myPersonName = myPerson.Name;
Address myPersonAddress = myPerson.Address;

I got an a person with an association named Address and a property named Name. If I modify manually the datas in database for example the property Name, I have to use the following code to reload my entity:

context.Entry(myPerson).Reload();

and I have the new value for Name. But If I do the same for Address it doesn't work. I think it is because Address is an association property. I need to refresh it.

How Can I do to force the reload of Address association (and all other association in Person class) ?

EDIT:

In the same case, a person can have more than one address.

MyContext context = new MyContext();

Person myPerson = context.PersonSet.FirstOrDefault();
String myPersonName = myPerson.Name;
List<Address> myPersonAddresses = myPerson.Addresses;

In this case, it is not a Reference:

context.Entry(myPerson).Reference(p => p.Address).Load();
// Address will be populated with only the new address
// this isn't required because I use lazy loading

but a Collection:

context.Entry(myPerson).Collection(p => p.Addresses).Load();
// Address will be populated with old value and new value

I need to use this to work:

context.Entry(myPerson).Collection(p => p.Addresses).CurrentValue.Clear();
context.Entry(myPerson).Collection(p => p.Addresses).Load();

But it doesn't seem to be a good solution to do this for all my navigation properties!

解决方案

If you don't use lazy loading, you have the load the new Address explicitly (as you had to load it explicitly (with Include, for example), when you loaded the Person initially):

context.Entry(myPerson).Reload();
// If the person refers to another Address in the DB
// myPerson.Address will be null now

if (myPerson.Address == null)
    context.Entry(myPerson).Reference(p => p.Address).Load();
    // myPerson.Address will be populated with the new Address now

If you use lazy loading, you don't need the second code block. Nonetheless, you get a new query to the database as soon as you access properties of the new myPerson.Address (like you have a new query in the second code block above) because the first line will mark the navigation property as not loaded if the person refers to a new address in the DB.

This behaviour doesn't depend on whether you have exposed the foreign key in the model class or not.

There doesn't seem to be a way to call some single magic Reload method which would reload and update the whole object graph in one call (similar like there is no single Include to eager load a complete object graph).

这篇关于重新加载实体和所有导航属性关联 - DbSet 实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)