我有两个类,如以下结构,Person与SchoolClass有2个多一关系:
public class Person
{
public int Id { get; set; }
public List<SchoolClass> SchoolClasses { get; set; }
public List<SchoolClass> OldSchoolClasses { get; set; }
}
public class SchoolClass
{
public int Id { get; set; }
public string Name { get; set; }
}
我可以添加ghostProps,但不能更改类。
当我为EFCore进行迁移时,它会在SchoolClass表中添加两个不同的Person Id,我希望避免这样做。
我想生成2个多对多表:
- 个人学校课程表
- OldPersonSchool课程表
使用Fluent API是否可以实现这一点?
也许您应该尝试继承:
public class SchoolClass // this could be abstract
{
public int Id { get; set; }
public string Name { get; set; }
}
public class NewSchoolClass : SchoolClass
{
}
public class OldSchoolClass : SchoolClass
{
}
public class Person
{
public int Id { get; set; }
public IList<NewSchoolClass> NewSchoolClasses { get; set; }
public IList<OldSchoolClass> OldSchoolClasses { get; set; }
}
在DbContext中:
-DbSet<SchoolClass> SchoolClasses { get; set; }
+DbSet<OldSchoolClass> OldSchoolClasses { get; set; }
+DbSet<NewSchoolClass> NewSchoolClasses { get; set; }