繁体   English   中英

EF代码优先:一对多两次到相同的集合类型

[英]EF code first: one-to-many twice to same collection type

简化:在我的数据库中,我有一个在不同日期以不同价格出售的产品。 换句话说,它有一个价格历史 我有两个类: 产品价格与一对多的关系:

public class Product
{
    public int ProductId {get; set;}
    public string Name {get; set;}

    public ICollection<Price> Prices {get; set;}
}

public class Price
{
    public int PriceId {get; set;}

    // foreign key to Product:
    public int ProductId {get; set;}
    public Product Product {get; set;}

    public DateTime ActivationDate {get; set;}
    public decimal value {get; set;}
}

public class MyDbContext : DbContext
{
    public DbSet<Price> Prices { get; set; }
    public DbSet<Product> Products { get; set; }
}

到目前为止,实体框架知道如何处理这个问题。 通过使用这两个类,我可以在特定日期获得某个产品的价格。

但是,如果我的产品有两个价格历史记录:购买价格历史记录和零售价格历史记录怎么办?

public class Product
{
    public int ProductId {get; set;}
    public string Name {get; set;}

    public ICollection<Price> PurchasePrices {get; set;}
    public ICollection<Price> RetailPrices {get; set;}  
}

因为这两个集合属于同一类型,所以我不希望单独的表填充相同类型的对象(真正的原因:我有很多带有价格集合的类)。

所以我必须使用Fluent API进行一些编码。 我的直觉是说我需要使用ManyToManyNavigationPropertyConfiguration.Map来连接表,就像在多对多关系中一样。

这个怎么做?

目前,由于EF命名约定,您的代码正在运行:

Code First推断,如果类上的属性名为“ID”(不区分大小写),或者类名后跟“ID”,则属性是主键。 如果主键属性的类型是数字或GUID,则它将配置为标识列。

EF看到你有一对多,所以它自动将ProductId作为外键。 如果要定义同一实体的多个集合,则必须手动定义外键。

public class Price
{
   public int PriceId {get; set;}

   public int ProductPurchaseId {get; set;}
   public Product ProductPurchase {get; set;}

   public int ProductRetailId {get; set;}
   public Product ProductRetail {get; set;}

   public DateTime ActivationDate {get; set;}
   public decimal value {get; set;}
}

并且流利的api:

modelBuilder<Product>().HasMany(p => p.PurchasePrices)
                       .WithRequired(p => p.ProductPurchase)
                       .HasForeignKey(p => p.ProductPurchaseId);

modelBuilder<Product>().HasMany(p => p.RetailPrices)
                       .WithRequired(p => p.ProductRetail)
                       .HasForeignKey(p => p.ProductRetailId);

这当然意味着您需要在Price表中为Product提供2个外键。

根据我对您的要求的理解,您需要在价格表中添加一个额外字段,该字段将告诉您要存储的价格类型。 例如:

public class Price
{
    public int PriceId {get; set;}

    // The PriceType will recognise among different type of price- Sell Price,          Purchase Price etc.

    public string PriceType{get;set;}

    // foreign key to Product:
    public int ProductId {get; set;}
    public Product Product {get; set;}

    public DateTime ActivationDate {get; set;}
    public decimal value {get; set;}
}

在阅读有关一对一外键关联的故事并将其用于一对多关联之后,我能够按照以下要求实现它:

  • 我可以有许多不同的类,具有相同类型T的属性
  • 类型T的所有实例都可以放在一个表中,即使这种类型的“所有者”在不同的表中。
    • 一个类甚至可以有两个类型为T的属性。

例如:客户可能有一个类型为Address的BillingAddress和DeliveryAddress。 两个地址都可以放在一个表中:地址。

public class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }

    public int BillingAddressId { get; set; }
    public Address BillingAddress { get; set; }
    public int DeliveryAddressId { get; set; }
    public Address DeliveryAddress { get; set; }
}

public class MyDbContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
            .HasRequired(p => p.DeliveryAddress)
            .WithMany()
            .HasForeignKey(p => p.DeliveryAddressId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<User>()
            .HasRequired(p => p.BillingAddress)
            .WithMany()
            .HasForeignKey(p => p.BillingAddressId)
            .WillCascadeOnDelete(false);
    }
}

这个解决方案的聪明之处在于Address中没有“拥有”用户。 因此,如果我使用Address定义一个新类,则可以将该地址添加到同一个Address表中。 所以,如果我有十个不同的类都有一个地址,我不需要十个地址表。

如果你有一组地址怎么办?

通常在一对多关系中,许多方需要一方的外键加上对“所有者”的引用:

一个经常看到的例子:博客和帖子:一个博客有很多帖子。 一个帖子只属于一个博客:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    virtual public ICollection<Post> Posts {get; set;}
}

public class Post
{
    public int Id { get; set; }
    public string Text { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
 }

此命名将自动导致正确的一对多关系,但如果要在DbContext中指定:

public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

并在OnModelCreating中:

modelBuilder.Entity<Blog>()
    .HasMany(b => b.Posts)
    .WithRequired(post => post.Blog)
    .HasForeignKey(post => post.BlogId);

即使您不需要Post.Blog,也无法删除此属性,因为模型创建。 如果要删除它,最终会使用魔术字符串来定义外键。

为了能够有一组地址(或在我原来的问题:很多价格历史,每个价格历史是价格的集合)我结合了这两种方法。

public class Price
{
    public int Id { get; set; }
    public int PriceHistoryId { get; set; }
    public virtual PriceHistory PriceHistory { get; set; }

    public DateTime ActivationDate { get; set; }
    public decimal Value { get; set; }
}

public class PriceHistory
{
    public int Id { get; set; }
    virtual public ICollection<Price> Prices { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    // Purchase Prices
    public virtual PriceHistory PurchasePriceHistory { get; set; }
    public int PurchasePriceHistoryId { get; set; }

    // Retail prices
    public virtual PriceHistory RetailPriceHistory { get; set; }
    public int RetailPriceHistoryId { get; set; }
}

public class MyDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<PriceHistory> PriceHistories { get; set; }
    public DbSet<Price> Prices { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // one price history has many prices: one to many:
        modelBuilder.Entity<PriceHistory>()
            .HasMany(p => p.Prices)
            .WithRequired(price => price.PriceHistory)
            .HasForeignKey(price => price.PriceHistoryId);

        // one product has 2 price histories, the used method is comparable
        // with the method user with two addresses
        modelBuilder.Entity<Product>()
            .HasRequired(p => p.PurchasePriceHistory)
            .WithMany()
            .HasForeignKey(p => p.PurchasePriceHistoryId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Product>()
            .HasRequired(p => p.RetailPriceHistory)
            .WithMany()
            .HasForeignKey(p => p.RetailPriceHistoryId)
            .WillCascadeOnDelete(false);
    }
}

我已经用其他具有多个价格历史的类进行了测试: - 所有价格都在一个表格中 - 所有价格历史记录都在一个表格中 - 每个价格历史记录都需要一个价格历史记录。

如果仔细观察结果,实际上是价格历史是耦合表的多对多关系的实现。

我试图删除PriceHistory类,并让一个Product在OnModelCreating中有多个具有多对多的价格集合,但这会导致带有魔术字符串的“Map”语句和每个PriceHistory的单独表。

链接到有关如何实现多对多的解释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM