[英]Entity Framework 4.1. Loading navigational properties from Ids
I have a bunch of properties like this on OrderItem
: 我在
OrderItem
上有很多这样的属性:
public virtual Frame Frame { get; set; }
[ForeignKey("Frame")]
public int? FrameId { get; set; }
I have a controller like this: 我有一个像这样的控制器:
public ActionResult CostOptions(OrderItem oi)
I am setting the Ids on oi
with model binding as above, now is there a way to get the navigational properties to load automatically from the Ids? 我如上所述通过模型绑定在
oi
上设置Ids,现在是否有办法让导航属性从Ids自动加载? Do I need to insert the entity to do this? 我需要插入实体来执行此操作吗?
The OrderItem
has to be a proxy created by EF inorder to load the navigational property pointed by the relevant id. OrderItem
必须是EF创建的代理,才能加载相关ID指向的导航属性。 Your current implementation does not allow this because MVC model binder creates the instance OrderItem
. 您当前的实现不允许这样做,因为MVC模型绑定程序创建了实例
OrderItem
。
public ActionResult CostOptions()
{
// creates instance of the proxy
var oi = db.OrderItems.Create();
if (TryUpdateModel(oi))
{
// new entity has to be added before retrieving lazy loaded prop
db.OrderItems.Add(oi);
// lazy loaded property
var frame = oi.Frame;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.