【问题标题】:How do we implement an IS-A relationship in Entity Framework?我们如何在实体框架中实现 IS-A 关系?
【发布时间】:2019-04-04 03:52:30
【问题描述】:

实体是ProgrammerEngineer,它们都是Employee。我想探索实体框架中的 IS-A 选项。请谁能告诉我如何使用实体框架(代码优先或数据库优先)实现“is-a”关系?

我有这样的课程:

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

public class Programmer : Employee
{
     double HRate { get; set; }
}

public class Engineer: Employee
{
     double MRate { get; set; }
}

【问题讨论】:

  • 您使用的是哪个版本的 EntityFramework?
  • 实体框架6最新款
  • 那么你可能会找到this link usefull

标签: c# .net entity-framework .net-core


【解决方案1】:

Is-A 关系是继承,在 EF 中实现它的一种可能方式如下:

public class Employee
{
     public int Id { get; set; }
     public string Name { get; set; }
}
public class Programmer 
{
     [ForeignKey(nameof(Employee))]
     public  int Id { get ; set ; }
     public virtual Employee {get;set;}
     double HRate { get; set; }

}
public class Engineer: Employee
{
     [ForeignKey(nameof(Employee))]
     public  int Id { get ; set ; }
     public virtual Employee {get;set;}
     double MRate { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多