[英]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.