重新加载实体和所有导航属性关联-DbSet实体框架 [英] Reload an entity and all Navigation Property Association- DbSet Entity Framework
问题描述
我对实体关联刷新有问题。当我得到这样的实体时:
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;
我得到一个人,这个人的关联名为Address,属性为Name。如果我手动修改数据库中的数据(例如属性Name),则必须使用以下代码重新加载我的实体:
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();
,我有了Name的新值。但是,如果我对地址执行相同操作,则无法正常工作。我认为这是因为Address是一个关联属性。我需要刷新它。
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.
如何强制重新加载地址关联(以及Person类中的所有其他关联)?
How Can I do to force the reload of Address association (and all other association in Person class) ?
编辑:
在同一情况下,一个人可以拥有多个地址。
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
但是集合:
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();
但是对于我所有的导航属性来说,这似乎都不是一个好的解决方案! / p>
But it doesn't seem to be a good solution to do this for all my navigation properties!
推荐答案
如果不使用延迟加载,则可以加载新的 Address
在加载 Person
Include )) >最初):
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
如果您使用lazy加载时,您不需要第二个代码块。但是,访问新的 myPerson.Address
的属性后,您会立即对数据库进行新查询(例如,在上面的第二个代码块中有一个新查询),因为如果此人引用数据库中的新地址,则第一行会将导航属性标记为未加载。
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.
似乎没有一种方法可以调用某些单个魔术重新加载
将在一次调用中重新加载和更新整个对象图的方法(类似于没有单个 Include
渴望加载完整的对象图的情况。)
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实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!