我如何在.Net Core中配置一对多关系

时间:2019-12-04 23:49:42

标签: c# asp.net-core mapping ef-core-2.0

我想在 Ef核心中配置一对多关系。如您所见,我有一个用于 order 的类,另一个是用于 OrderItems 的类。 我使用NHibernate时会这样做。当然,我将orderItem类视为ValueObject。但是我想使用EF Core。

public class Order 
{
    public long Id { get; set; }
    public long CustomerId { get; set; }
    public DateTime OrderDateTime { get; set; }

    public ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem  
{
    public string BookId { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal? Discount { get; set; }
    public decimal Total { get; set; }

    public Order Order { get; set; }

}

3 个答案:

答案 0 :(得分:2)

您应该在订单实体中使用主键类型定义元数据[ForeignKey]。之后,该ef内核会根据您选择的名称自动在db中设置

public class Order 
{
    public long Id { get; set; }
    public long CustomerId { get; set; }
    public DateTime OrderDateTime { get; set; }

    public virtual ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem 
{
    public long Id { get; set; }    

    [ForeignKey(nameof(OrderId)]
    public virtual Order Order { get; set; }
    public long OrderId { get; set; }

    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal? Discount { get; set; }
    public decimal Total { get; set; }
}

答案 1 :(得分:1)

  

型号

 public class Order     {
            public long Id { get; set; }
            public long CustomerId { get; set; }
            public DateTime OrderDateTime { get; set; }
            public virtual ICollection<OrderItem> OrderItems { get; set; }
      }

public class OrderItem

{  
    [Key]
    public int OrderItemId { get; set; }
    public string BookId { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal? Discount { get; set; }
    public decimal Total { get; set; }
    public long OrderId{get;set;} // ForeignKey OrderId
    [ForeignKey("OrderId")]
    public virtual Order Order { get; set; }

}

//打开PackageManager控制台

PM>add-migration "orderItem changed"
PM>update-database

答案 2 :(得分:0)

这是一个简单的演示,如下所示:

1。型号:

public class Order
{
    public long Id { get; set; }
    public long CustomerId { get; set; }
    public DateTime OrderDateTime { get; set; }

    public ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
    public int OrderItemId { get; set; }//you need to define a primary key for OrderItem model
    public string BookId { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal? Discount { get; set; }
    public decimal Total { get; set; }

    public Order Order { get; set; }

}

2.DbContext:

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }

    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderItem> OrderItems { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Order>()
                    .HasMany(c => c.OrderItems)
                    .WithOne(e => e.Order);
    }
}

3.Startup.cs:

services.AddDbContext<MyDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));

4.appsettings.json:

"ConnectionStrings": {
   "MyDbContext": "Server=(localdb)\\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true" 
}

5。在Package Nuget Manager上运行命令行:

PM>add-migration init
PM>update-database