【问题标题】:Implementing Class Level inheritance with t4scafolding使用 t4scafolding 实现类级继承
【发布时间】:2013-12-05 07:15:22
【问题描述】:

我正在测试一个具有多个项目的项目,如下所示: 1.项目.Mvc 2. Project.DomainClasses 3. Project.DataLayer

在 Project.DomainClasses 中,我实现了类级别继承,如下所示

[Table("Person")]
public class Person
{
    [Key]
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
}

[Table("Employee")]
public class Employee:Person
{
    [ForeignKey("EmployeeId")]
    public int EmployeeId { get; set; }
}

然后我通过包管理器控制台安装了 t4scaffolding。之后,我为 DataLayer 创建了另一个名为 Project.DataLayer 的项目,安装了实体框架并引用了 Project.DomainClasses。现在,当我尝试使用 t4scafold 在包管理器控制台中使用以下命令创建上下文时:

Scaffold Repository Project.DomainClasses.Employee -DbContextType:EmployeeContext 

我收到以下错误:


Get-PrimaryKey:找不到类型 Project.DomainClasses.Employee' 的主键属性。多个属性似乎是主键:PersonId、EmployeeId 在 C:\Project\packages\T4Scaffolding.1.0.8\tools\EFRepository\T4Scaffolding.EFRepository.ps1:19 char:29 + $primaryKey = Get-PrimaryKey


这是我第一次尝试在数据库级别实现继承。在实现这一点之前,我浏览了 msdn 和 stackoverflow 中的不同文章。但是,我现在被困住了。 提前感谢您帮助我和您的时间。

【问题讨论】:

  • 我对不使用继承的模型进行了同样的尝试,效果很好。

标签: c# entity-framework asp.net-mvc-4 inheritance


【解决方案1】:

解决方案非常简单。我对派生类的主键感到困惑。由于数据库应该具有主键的派生类,它也充当基类的外键,我试图添加 EmployeeId 并将其声明为导致问题的外键。所以,解决办法是:

[Table("Employee")]
public class Employee:Person
{
// Remove EmployeeID property
//Add other properties if exists else leave it as empty class
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多