实体框架6将属性添加到联接表 [英] Entity Framework 6 Adding properties to join tables

查看:93
本文介绍了实体框架6将属性添加到联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是方案: 我有一个产品表和一个类别表.这种关系是多对多的:一个类别可以有1个或更多产品...,而一个产品可以是1个或多个类别...

This is the scenario: I have a products table and a categories table. The relationship is many-to-many: a category can have 1 or more products....and a product can be in 1 or more categories...

代码优先的映射看起来像这样....

The Code-First mapping looks like this....

public class Product
{
  //...additional properties...
  public virtual ICollection<Category> AssociatedCategories {get; set;}
}

public class Category
{
  //...additional properties...
  public virtual ICollection<Product> AssociatedProducts {get; set;}
}

现在,实体框架将在幕后创建一个名为ProductCategory的联接表,该联接表的列为ProductID和CategoryID.太好了...

Now, under the hood, entity framework will create a join table called ProductCategory with columns ProductID and CategoryID. That's great....

这就是问题,我需要引入一个排序顺序...基本上只是一个基本的定位索引,但是这个数字仅存在于产品和类别彼此结合的关系中.例如,产品X在类别Y中的排序顺序值可能是"5",但是某些产品X在类别Z中的排序值可能会不同(例如10).

Here's the thing though, I need to introduce a sort order...basically just a cardinal positioning index, but this number exists only at the part in the relationship where product and category meet each other. For example, a product X might have a sort order value of "5" in Category Y, but that some product--X--could have a different sort value--say 10--in Category Z.

自然地,我可以专门为这种类型的东西创建一个实体...但是它需要创建一个新表...类别ID,产品ID和排序顺序将有3列.我真正想做的是进入实体框架已经建立的表....它将已经跟踪联接表中的产品ID和类别ID ...是否有任何方法可以使用已经存在的表?

Naturally, I could create an entity specifically for this type of thing...but it would require a new table be made...there would be 3 columns for the Category ID, Product ID, and sort order. What I'd really like to be able to do is tap into the table that entity framework already made....it will already keep track of products IDs and category IDs in the join table....is there any way to make use of the table that already exists?

推荐答案

您需要为此联接表创建一个特定的实体.

You need to create a specific entity for the join table in order to do this.

public class Product
{
  //...additional properties...
  public virtual ICollection<ProductCategoryXref> AssociatedCategories {get; set;}
}

public class Category
{
  //...additional properties...
  public virtual ICollection<ProductCategoryXref> AssociatedProducts {get; set;}
}

public class ProductCategoryXref
{
    public int ProductId { get; set; }
    public int CategoryId { get; set; }
    public int SortOrder { get; set; }
    // Additional Columns...

    public virtual Product Product { get; set; }
    public virtual Category Category { get; set; }
}

如果您使用Fluent API来配置您的实体,它将看起来像这样:

If you are using the Fluent API to configure your entities it will look something like this:

 public class ProductCategoryXrefMap : EntityTypeConfiguration<ProductCategoryXref>
 {
      ProductCategoryXrefMap()
      {
           HasKey(pk => new { pk.ProductId, pk.CategoryId });
           HasRequired(p => p.Product).WithMany(p => p.AssociatedCategories).HasForeignKey(fk => fk.ProductId);
           HasRequired(p => p.Category).WithMany(p => p.AssociatedProducts).HasForeignKey(fk => fk.CategoryId);
           ToTable("ProductCategoryXref");
      }
 }

这篇关于实体框架6将属性添加到联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆