【问题标题】:Entity Framework 4/MVC3 navigation properties dilemmaEntity Framework 4/MVC3 导航属性困境
【发布时间】:2011-07-30 02:30:57
【问题描述】:

我试图弄清楚如何为将生成 SQL 表的 2 个模型(实体)设置导航属性。场景:我有一个货物和一个公司模型/实体。我需要将 Shipment 模型中的 3 个属性 ClientID¸ ShipperID 和 ConsigneeID 关联到 Company 模型中的 CompanyID。现在,Shipment 模型的正确导航属性是什么?上下文会是什么样子?

    public virtual ICollection< Company > Companies { get; set; }
        OR
    public virtual Company Company { get; set; }

以下是 2 个模型:

    public class Shipment
{
    public int ShipmentID { get; set; }
    public string Name { get; set; }
    public DateTime DateStamp { get; set; }
    public int ClientID { get; set; }
    public int ShipperID { get; set; }
    public int ConsigneeID { get; set; }

    public virtual ICollection< Company > Companies { get; set; }
        OR
    public virtual Company Company { get; set; }
}

public class Company
{
    public int CompanyID { get; set; }
    public string Name { get; set; }
    public DateTime DateStamp { get; set; }

    public virtual ICollection< Shipment > Shipments { get; set; }
}

【问题讨论】:

    标签: entity-framework asp.net-mvc-3


    【解决方案1】:

    您需要使用一些属性来完成此操作。 我假设您在发货和公司之间存在 1-* 关系。 (发给 * 客户/托运人/收货人的货物) 发货:

    public class Shipment
    {
        public int ShipmentID { get; set; }
        public string Name { get; set; }
        public DateTime DateStamp { get; set; }
        [ForeignKey("Client")]
        public int ClientID { get; set; }
        [ForeignKey("Shipper")]
        public int ShipperID { get; set; }
        [ForeignKey("Consignee")]
        public int ConsigneeID { get; set; }
    
        public virtual Company Client { get; set; }
        public virtual Company Shipper { get; set; }
        public virtual Company Consignee { get; set; }
    }
    

    公司:

    public class Company
    {
        public int CompanyID { get; set; }
        public string Name { get; set; }
        public DateTime DateStamp { get; set; }
        [InverseProperty("Shipper")]
        public virtual ICollection< Shipment > ShipmentsShipped { get; set; }
        [InverseProperty("Consignee")]
        public virtual ICollection<Shipment> ShipmentsConsigned { get; set; }
        [InverseProperty("Client")]
        public virtual ICollection<Shipment> ShipmentsOwned { get; set; }
    }
    

    背景:

    public class TesteEntityMVCContext : DbContext
    {     
        public DbSet<Shipment> Shipments { get; set; }
        public DbSet<Company> Companies { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      如果您的货物有许多您需要使用的公司(多对多关系)

      public virtual ICollection< Company > Companies { get; set; }
      

      否则,如果您的货件只有一家您需要使用的公司(一对多关系)

        public virtual Company Company { get; set; }
      

      您还可以选择在您的 dbContext 中指定更多关于 onModelBuilding 事件中的关系。

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
        modelBuilder.Entity<Shipment>()
          .HasRequired(x => x.Company ) \\..... etc as your requirement
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        • 2017-01-01
        相关资源
        最近更新 更多