[英]Multiple collections of same type in entity framework
I have these two very simple classes.我有这两个非常简单的类。
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
}
public class Group
{
public int Id {get;set;}
public ICollection<Person> Teachers { get; set; }
public ICollection<Person> Students { get; set; }
}
I would like EF to keep Teachers
seperated from Students
however they both get jumbled into a Person
table with no way to distinguish between them.我希望 EF 将
Teachers
与Students
分开,但是他们都混入了一个Person
表,无法区分它们。
Any ideas?有任何想法吗?
There are two ways to do it;有两种方法可以做到;
first : use a tag or enums in the Person
object第一:在
Person
对象中使用标签或枚举
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
public bool IsFaculty { get; set; }
}
or或者
public enum PersonType { Teacher, Student };
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
public PersonType PropPersonType { get; set; }
}
second : work object oriented with inheritance.第二:工作面向对象继承。 This method has my preference because it's easy to manage and expand if you want to expand it.
这种方法有我的偏好,因为如果你想扩展它,它很容易管理和扩展。
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
}
public class Student : Person
{
public int Year { get; set; }
// other student related fiels.
}
public class Teacher : Person
{
public List<Course> Courses { get; set; }
// other teacher related fields
}
Your Group
is then你的
Group
然后
public class Group
{
public int Id {get;set;}
public ICollection<Teacher> Teachers { get; set; }
public ICollection<Student> Students { get; set; }
}
Separate in two different classes, and then inherit both of them with Person, because all the teachers and students are Persons, but not all Persons are teachers and students.分开在两个不同的类中,然后用Person继承它们,因为所有的老师和学生都是Person,但并不是所有的Person都是老师和学生。
public class Person
{
}
public class Teacher : Person
{
}
public class Student : Person
{
}
I hope this helps我希望这有帮助
A little bit different case (when class is holding reference to two identical objects), but can be helpful:有点不同的情况(当类持有对两个相同对象的引用时),但可能会有所帮助:
public class Mission
{
//DB objects
public int Id { get; set; }
public int SourceLocationId {get; set}
public int DestinationLocationId {get; set}
//Virtual objects
public virtual Location SourceLocation { get; set; }
public virtual Location DestinationLocation { get; set; }
}
public class Location
{
//DB objects
public int Id {get;set;}
//Virtual objects
public virtual ICollection<Mission> SourceMissions { get; set; }
public virtual ICollection<Mission> DestinationMissions { get; set; }
}
Then all you need to do is bind it properly in OnModelCreating :然后你需要做的就是在OnModelCreating 中正确绑定它:
modelBuilder.Entity<Mission>()
.HasOptional(m => m.SourceLocation) //Optional or Required
.WithMany(sm => sm.SourceMissions)
.HasForeignKey(to => to.SourceLocationId);
modelBuilder.Entity<Mission>()
.HasOptional(m => m.DestinationLocation) //Optional or Required
.WithMany(sm => sm.DestinationMissions)
.HasForeignKey(to => to.DestinationLocationId);
To have Teachers and Students in two separate tables inherit from an abstract base:要让两个单独的表中的教师和学生从抽象基础继承:
public abstract class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
}
public class Student : Person
{
public int Year { get; set; }
// other student related fiels.
}
public class Teacher : Person
{
public List<Course> Courses { get; set; }
// other teacher related fields
}
public class Group
{
public int Id {get;set;}
public ICollection<Teacher> Teachers { get; set; }
public ICollection<Student> Students { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.