C# 实体框架中相同类型的多个集合

C# 实体框架中相同类型的多个集合,c#,entity-framework,C#,Entity Framework,我有两个非常简单的类 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 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; }
}
公共类人物
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共组组{get;set;}
}
公共课组
{
公共int Id{get;set;}
公共ICollection教师{get;set;}
公共ICollection学生{get;set;}
}
我希望EF将
教师
学生
分开,但他们都被混在
个人
表格中,无法区分他们


有什么想法吗

分为两个不同的班级,然后与人一起继承,因为所有的老师和学生都是人,但不是所有的人都是老师和学生

public class Person
{
}

public class Teacher : Person
{
}

public class Student : Person
{
}

我希望这能有所帮助,我想你需要一些标志来区分它们(我真不敢相信你不能)。之后,您可以使用
TPH
继承方法。查看更多信息和信息。

有两种方法

first:在
Person
对象中使用标记或枚举

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Group Group { get; set; }
    public bool IsFaculty { get; set; }
}

第二个:使用继承进行面向对象的工作。我喜欢这种方法,因为如果您想扩展它,它很容易管理和扩展

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
}

教师学生分为两个独立的表,从抽象基础继承:

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; }
}
公共抽象类人物
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共组组{get;set;}
}
公营班级学生:人
{
公共整数年{get;set;}
//其他与学生有关的领域。
}
公开课教师:人
{
公共列表课程{get;set;}
//其他教师相关领域
}
公共课组
{
公共int Id{get;set;}
公共ICollection教师{get;set;}
公共ICollection学生{get;set;}
}

情况稍有不同(当类持有对两个相同对象的引用时),但可能会有所帮助:

    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; }
}
公共类任务
{
//数据库对象
公共int Id{get;set;}
public int SourceLocationId{get;set}
public int DestinationLocationId{get;set}
//虚拟对象
公共虚拟位置SourceLocation{get;set;}
公共虚拟位置DestinationLocation{get;set;}
}
公共类位置
{
//数据库对象
公共int Id{get;set;}
//虚拟对象
公共虚拟ICollection SourceMissions{get;set;}
公共虚拟ICollection DestinationMissions{get;set;}
}
然后,您需要做的就是在ModelCreating上正确绑定它:

   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);
modelBuilder.Entity()
.has可选(m=>m.SourceLocation)//可选或必需
.WithMany(sm=>sm.SourceMissions)
.HasForeignKey(to=>to.SourceLocationId);
modelBuilder.Entity()
.has可选(m=>m.DestinationLocation)//可选或必需
.WithMany(sm=>sm.DestinationMissions)
.HasForeignKey(to=>to.DestinationLocationId);

是的,我已经考虑过这样做,但如果可能的话,我真的想避免这样做。
    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; }
}
   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);