简体   繁体   English

使用实体框架重用多对多表

[英]Reuse Many to Many Table with Entity Framework

Greetings i have following Models: 问候我有以下模型:

public class Product 
{
    public Guid Id { get; set; }
    public String Title { get; set; }
}


public class Category
{
    public Guid Id { get; set; }
    public String Name { get; set; }
}


public class Language 
{
    public Guid Id { get; set; }
    public String Title { get; set; }
}

Language has many to many relation to Product And also Category. 语言与产品和类别有很多关系。 the traditional way is you create Language_Product Table and Language_Category table but I was wondering if it's possible to create a single table so I won't make duplicate Many-to-Many tables for other models that have many to many relation with my Language. 传统的方式是您创建Language_Product表和Language_Category表,但是我想知道是否可以创建一个表,这样我就不会为与我的语言有多对多关系的其他模型复制多对多表。 something like below (keep in mind their IDs are same datatype): 如下所示(请注意,其ID是相同的数据类型):

LanguageId                             GroupId     
-------                                ----     
a2b4a2d1255540aeb6c41e4309e14a80       4cf16dd0baa9424e89f7bb43deefd0cc <-- Product
95870ac828c84937baab63ad39b4f420       4cf16dd0baa9424e89f7bb43deefd0cc <-- Product
95870ac828c84937baab63ad39b4f420       55c884375d304f958fa6b4dde1d29559 <-- Category
...

Also if it's possible then Does EF understand the relation and will i be able to get the data same way i used to create many-to-many relations as before in code-first? 另外,如果可能的话,EF是否可以理解这种关系,我是否能够像以前那样以代码优先的方式获取用于创建多对多关系的数据?

You shouldn't have a single table for the many-to-many relationship - that will break your db model, and you won't be able to implement data integrity properly, ie you won't be able to use foreign keys. 您不应为多对多关系使用单个表-这会破坏您的数据库模型,并且您将无法正确实现数据完整性,即,您将无法使用外键。

Better, have a single table for the data like categories and titles, and add a column to know which record which type is. 更好的是,为诸如类别和标题之类的数据提供一个表,并添加一列以了解哪种类型的记录。 Then, you will have also a single many-to-many table. 然后,您还将有一个单一的多对多表。

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

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