简体   繁体   English

实体框架4.1。 从ID加载导航属性

[英]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.

相关问题 实体框架核心包括加载额外的导航属性 - Entity Framework Core include loading extra navigational properties 实体框架自定义导航属性 - Entity Framework Custom Navigational Properties Entity Framework 6代码优先中的多维导航属性 - Multidimensional navigational properties in Entity Framework 6 Code First 实体框架中的Restsharp和关系/导航属性 - Restsharp and relations / navigational properties in Entity Framework 实体框架:导航属性-代码优先 - Entity Framework: Navigational properties - Code First 阻止实体框架为导航属性插入值 - Prevent Entity Framework to Insert Values for Navigational Properties 实体框架3-链式导航属性中的过滤器元素 - Entity Framework 3 - Filter Elements in Chained Navigational Properties 实体框架4.1。 更新多对多关系。 这是正确的方法吗? - Entity Framework 4.1. Updating many-to-many relationships. Is this the right way? 如果我从数据库中删除外键约束,那么实体框架的导航属性会起作用吗? - Would the Entity Framework's navigational properties work if I drop foreign key constraints from the database? 使用实体框架时如何填充导航属性 - how to populate navigational properties when using Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM