3

Consider the following model:

public partial class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public partial class Teacher : Person
{
    public string ClassName { get; set; }
}

public partial class Student : Person
{
    public int NumberOfClasses { get; set; }
}

Using that model and Entity Framework, is it possible to have a "Student" instance and a "Teacher" instance both derived from the same base "Person" instance? In other words, can a "Person" be both a "Student" and a "Teacher"?

If so, what would be the best inheritance strategy to use to represent this scenario?

4

2 回答 2

1

“抽象”关键字是关键。不知道你为什么使用“部分”。您需要先选择一个策略 - 此链接说明了所有内容。http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application

public abstract class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Teacher : Person
{
    public string ClassName { get; set; }
}

public class Student : Person
{
    public int NumberOfClasses { get; set; }
}

*编辑 - 示例用法 *

public void GetSomeDetailAboutAPerson(Person person)
{
    return person.SomeSharedDetailFromBaseClass;
}

public void Something()
{
    Teacher teacher = myService.GetTeacherById(3);
    var someDetailOrOther = this.GetSomeDetailAboutAPerson(teacher);
}
于 2013-03-13T13:21:28.967 回答
-1

是的,有可能……一个人可能同时是学生或老师。

“继承策略”到底是什么意思?

你这样做的方式,没问题。

检查这个: http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc -应用

于 2013-03-13T13:20:47.877 回答