gpt4 book ai didi

c# - Entity Framework 中相同类型的多个集合

转载 作者:太空狗 更新时间:2023-10-29 17:50:41 25 4
gpt4 key购买 nike

我有这两个非常简单的类。

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; }
}

我希望 EF 将 TeachersStudents 分开,但是他们都混入了 Person 表中,无法区分它们.

有什么想法吗?

最佳答案

有两种方法可以做到;

首先:在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 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 : 面向工作对象的继承。这种方式是我比较喜欢的,因为管理方便,想扩容就扩容。

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
}

你的 Group

public class Group
{
public int Id {get;set;}
public ICollection<Teacher> Teachers { get; set; }
public ICollection<Student> Students { get; set; }
}

关于c# - Entity Framework 中相同类型的多个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com