简体   繁体   English

如何在EF中指向来自不同实体的同一实体的一对多关系建模

[英]How to Model One to Many relationship pointing to same entity from different entities in EF

I have an entity as Student 我有一个实体作为学生

public class Student{

..

 public ICollection<Picture> Pictures {get;set;}

} }

And School entity 和学校实体

public class School{

..

public ICollection<Picture> Pictures {get;set;}
}

When I create this model and run the migrations I can see that in Pictures table in the database 2 foreign key properties have been created as StudentId and SchoolId. 创建该模型并运行迁移时,我可以看到在数据库的Pictures表中已创建了2个外键属性,分别为StudentId和SchoolId。

It doesn't look right to me to have those foreign keys in the Pictures table. 在Picture表中具有这些外键对我来说似乎不对。

In the future if another entity needs to have list of Pictures then which means another foreign key property in the Pictures table. 将来,如果另一个实体需要“图片”列表,则意味着“图片”表中的另一个外键属性。

How can I change this behavior in the code first model? 如何在代码优先模型中更改此行为? Or this is the correct approach? 还是这是正确的方法?

If a picture cannot be shared among a school and a student, you could create separate entities for the two picture types. 如果无法在学校和学生之间共享图片,则可以为这两种图片类型创建单独的实体。 The best way to do this is to create a base class and derived classes for each specific picture type: 最好的方法是为每种特定的图片类型创建基类和派生类:

public abstract class Picture 
{
    public int Id { get; set; }
    // ...
}
public class SchoolPicture : Picture
{
    // ...
}
public class StudentPicture : Picture
{
    // ...
}

Then the School entity references the SchoolPicture and the Student entity references the StudentPicture: 然后,School实体引用SchoolPicture,而Student实体引用StudentPicture:

public class School
{
    // ...
    public virtual ICollection<SchoolPicture> Pictures {get;set;}
}
public class Student
{
    // ...
    public virtual ICollection<StudentPicture> Pictures {get;set;}
}

This way, two separate tables are created for the pictures with only a single foreign key to the corresponding parent entity: 这样,将为图片创建两个单独的表,只用一个外键指向相应的父实体:

创建模型

In your Student model, you are saying there is a one-to-many relationship between Student and Picture. 在学生模型中,您说的是学生与图片之间存在一对多的关系。 Same thing with your School model. 与您的School模型相同。 So EF adds a foreign key to Picture for both Student and School model. 因此,EF为“学生”和“学校”模型都向“图片”添加了外键。 That is the expected behavior. 那是预期的行为。

It doesn't look right to me to have those foreign keys in the Pictures table. 在Picture表中具有这些外键对我来说似乎不对。

Based on your Student and School model, I expect EF to add 2 foreign keys to Picture (StudentId and SchoolId). 根据您的学生和学校模型,我希望EF为图片添加2个外键(StudentId和SchoolId)。 How should it look to you? 看起来怎么样?

In the future if another entity needs to have list of Pictures then which means another foreign key property in the Pictures table. 将来,如果另一个实体需要“图片”列表,则意味着“图片”表中的另一个外键属性。

If you create another model that contains ICollection <Picture> Pictures { get; set; } 如果创建另一个包含ICollection <Picture> Pictures { get; set; } ICollection <Picture> Pictures { get; set; } ICollection <Picture> Pictures { get; set; } as a property, then yes, that would create another foreign key in Picture. ICollection <Picture> Pictures { get; set; }作为属性,然后是,它将在Picture中创建另一个外键。 But if you omit it, it won't. 但是,如果您忽略它,它不会。

I'm not sure what you are expecting. 我不确定您的期望。 Can you elaborate? 你能详细说明吗?

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

相关问题 EF Core-如何为已经存在一对多关系的同一实体设置多对多 - EF Core - How to setup many-to-many for same entity that already has a one-to-many relationship EF 7:如何以一对多关系加载相关实体 - EF 7: How to load related entities in a One-to-many relationship 有没有办法从拥有的 EF 实体创建一对多关系? - Is there a way to create a one to many relationship from an owned EF entity? EF Core - 如何通过一对多关系自引用 model? - EF Core - How to self reference a model with one to many relationship? EF多对多关系未添加实体之一的记录 - EF many to many relationship is not adding records for one of the entities 如何在 .NET EF Code First 中加入属于与另一个实体一样多的关系的实体的列或属性 - How to join columns or properties of entities that belongs as many relationship to another entity in .NET EF Code First 如何在EF 6中以多对多关系插入模型 - How to insert a model in ef 6 with a many to many relationship EFCore同一实体的多对多关系 - EFCore Many/One to Many relationship of the same entity EF6同一实体之间一对多和多对一 - EF6 One to Many and Many to One between same entities 实体类型不能共享同一个表 (EF6)(一对多关系) - Entity Types Cannot Share the same table (EF6) (One to Many Relationship)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM