实体框架5不清晰的导航性能 [英] Entity Framework 5 does not clear navigation property
问题描述
我有这个奇怪的问题与实体框架5,我有一个导航属性在我的实体,我想设置为空
之一。但由于某些原因的属性只被清除了我第二次拨打该属性:
I've got this strange issue with Entity Framework 5 where I have a navigation property in one of my entities that I would like to set to null
. But for some reason the property only gets cleared the second time I call this property:
using (var db = new Entities())
{
var vehicle = db.Vehicles.Single(v => v.Id == vehicleId);
// After this call, ParkingBay is still set.
vehicle.ParkingBay = null;
// Only after this call, ParkingBay becomes null.
vehicle.ParkingBay = null;
db.SaveChanges();
}
在车
类,实体框架生成这个样子的:
The Vehicle
class that Entity Framework generates looks like this:
public partial class Vehicle
{
public int Id { get; set; }
public System.DateTime CreatedOn { get; set; }
public int CreatedBy { get; set; }
public virtual ParkingBay ParkingBay { get; set; }
}
这code不是很退出,并在运行时的实体框架生成代理类为车
和 ParkingBay
,但我不明白, ParkingBay
属性,不能清除在第一次调用属性里面发生了什么。
This code isn't very exiting, and at runtime Entity Framework generates proxy classes for both Vehicle
and ParkingBay
, but I can't understand what happens inside that ParkingBay
property that fails to clear the property on the first call.
在车
和 ParkingBay
是SQL Server的一个正常的外键关系。这里没有什么特别。
Between Vehicle
and ParkingBay
is a normal Foreign Key relationship in SQL Server. Nothing special here.
更新
在 ParkingBay
是这样的:
public partial class ParkingBay
{
public ParkingBay()
{
this.Vehicles = new HashSet<Vehicle>();
}
public int Id { get; set; }
public System.DateTime CreatedOn { get; set; }
public int CreatedBy { get; set; }
public virtual ICollection<Vehicle> Vehicles { get; set; }
}
这是怎么回事呢?有人可以启发我,我做错了什么?
What's going on here? Can someone enlighten me what I'm doing wrong?
推荐答案
查询语句后( db.Vehicles.Single ...
)属性的是的空,因为你不加载它。指定另一个值,以它不会触发延迟加载,所以没有什么变化在这里。
After the query statement (db.Vehicles.Single ...
) the property is null, because you don't load it. Assigning another value to it doesn't trigger a lazy load, so nothing changes here.
只有当财产被实际加载的分配(任何的分配,也取代它被另一个对象)会产生作用。如果未装载属性,则变更跟踪器无关跟踪
Only when the property is actually loaded an assignment (any assignment, also replacing it by another object) will have an effect. If the property isn't loaded, the change tracker has nothing to track.
这个属性可以通过把它包含在查询得到加载
The property can get loaded by including it in the query
db.Vehicles.Include(v => v.ParkingBay)...
或以后在code解决这个问题,例如:
or by addressing it later in the code, e.g.
var pb = vehicle.ParkingBay; // triggers lazy loading.
,或通过检查其在调试器(手表或快速查看),这也触发延迟加载。
or by inspecting it in the debugger (watch or quickview), which also trigger lazy loading.
包含
是推荐的方法,如果你打算申请任何更改导航属性本身。
Include
is the recommended approach if you intend to apply any changes to navigation properties themselves.
如下评论说,一个性能更好的方式来清除参考导航属性是揭露在模型中的原始外键值并将其设置为空
。对你来说,这将是像诠释? ParkingBayId
。这种模式是知道的的外键关联的,而不是的独立协会的,当时只有参考属性为present。
As commented below, a better performing way to clear a reference navigation property is to expose the primitive foreign key value in the model and set it to null
. In your case, this would be something like int? ParkingBayId
. This pattern is know as foreign key associations, as opposed to independent associations, when only the reference property is present.
这篇关于实体框架5不清晰的导航性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!