c# - 如何定义一个表,其主键由 2 个外键构成,EF 代码优先

标签 c# entity-framework entity-framework-4 ef-code-first entity-framework-migrations

我正在使用模型通过 EF 代码优先定义我的表。

我有 Item 模型和 Order 模型。

项目:

 public class Item
    {
        public int ItemID { get; set; }

        [Required]
        public int Price { get; set; }

        [Required]
        public int AmountLeft { get; set; }

        [Required]
        public string Image { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public string FullDescription { get; set; }

        [Required]
        public DateTime PublishDate { get; set; }


        public int CompanyID { get; set; }
        public int CategoryID { get; set; }

        // Navigation properties 
        public virtual Company Company { get; set; }

        // Navigation properties 
        public virtual Category Category { get; set; } 

    }

订购型号:

public class Order
    {
        public int OrderID { get; set; }

        [Required]
        public DateTime DeliveryDate { get; set; }

        [Required]
        public string Currency { get; set; }

        [Required]
        public int TotalAmount { get; set; }

        public List<int> Items { get; set; }


        public DateTime OrderDate { get; set; }


        public int UserID { get; set; }
        // Navigation properties 
        public virtual User user { get; set; }

    }

我想创建另一个名为 ItemInOrder 的表,它只有 2 个字段:ItemID 和 OrderID。 主键将是这 2 个外键。

我试图定义这个模型:

   public class ItemInOrder
    {

        public int OrderID { get; set; }
        public int ItemID { get; set; }

        // Navigation properties 
        public virtual Order order { get; set; }
        public virtual Item item { get; set; }

    }

但是我有错误。我尝试在两个字段上都添加 [Key] 符号,但仍然出现错误。

我怎样才能创建我想要的表?

最佳答案

当您需要创建一个包含复合 PK 的表时,您需要指定键的顺序。有两种变体:

您可以重写 Context 上的 OnModelCreating 方法,并尝试使用这些 Fluent Api配置:

// Configure the primary keys for the ItemInOrder in the order you want
modelBuilder.Entity<ItemInOrder>() 
    .HasKey(t => new{t.OrderID,ItemID); 

modelBuilder.Entity<ItemInOrder>() 
            .HasRequired(io=>io.Order)
            .WithMany()
            .HasForeigKey(io=>io.OrderID);

 modelBuilder.Entity<ItemInOrder>() 
           .HasRequired(io=>io.Item)
           .WithMany()
           .HasForeigKey(io=>io.ItemID);

第二个变体使用 Data Annotations应该是这样的:

[Key] 
[Column(Order=1)] 
[ForeignKey("Order")]
public int OrderID { get; set; }

[Key] 
[Column(Order=2)] 
[ForeignKey("Item")]
public int ItemID { get; set; }

EF 会注意到您想要创建两个关系,它会为您完成这项工作。

关于c# - 如何定义一个表,其主键由 2 个外键构成,EF 代码优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555190/

相关文章:

c# - Entity Framework 4 - 删除对象

c# - 设置 Azure 推送通知中心无限设备过期

c# - Winform webbrowser reCAPTCHA 不显示

c# - 没有无限循环的堆栈溢出异常(据我所知)

asp.net - 您将如何将 Entity Framework (1.0) 与 ASP.Net Membership 一起使用?

c# - Quartz.net 的 Unity LifeTime 管理器

c# - 如何创建具有 EntityState 未知关系的对象

c# - ConfigureAwait 是否只影响非线程池线程?

c# - 如何创建一个只执行一次的方法?

c# - 部署到 Azure - 无法连接到数据库