【问题标题】:Table-per-type inheritance with the Entity Framework Reverse POCO Generator使用实体框架反向 POCO 生成器的按类型继承表
【发布时间】:2017-05-19 06:29:37
【问题描述】:

是否可以在table-per-type 继承场景中使用(很棒的)EntityFramework Reverse POCO Generator 来生成 POCO?

我的数据库包含一个“基本”日志表,以及从它派生的两个表:

create table LogBase
(
    Id int identity(1, 1) not null,
    LogTime datetime not null default getdate(),
    constraint PK_LogBase primary key clustered(Id)
) 

create table ErrorLog
(
    Id int not null,
    ErrorMessage nvarchar(max),
    StackTrace nvarchar(max),
    constraint PK_ErrorLog primary key(Id),
    constraint FK_ErrorLog_LogBase foreign key(Id) references LogBase(Id)
)

create table ChangeLog
(
    Id int not null,
    PropertyName nvarchar(max),
    OldValue nvarchar(max),
    NewValue nvarchar(max),
    constraint PK_ChangeLog primary key(Id),
    constraint FK_ChangeLog_LogBase foreign key(Id) references LogBase(Id)
)

默认情况下,反向 POCO 生成器会生成 3 个 C# 类——LogBase、ErrorLog 和 ChangeLog——每一个都包含一个Id 属性,并且它们之间没有继承关系。

我可以通过将类创建为部分类并将 : LogBase 继承放入部分类中来指定 ErrorLog 和 ChangeLog 从 LogBase 继承 - 这是指定继承的正确方法吗? p>

在模板生成器中,UpdateColumn 回调允许我指定应该在生成的 POCO 中省略其 Id 列的表。

我可以将 UpdateColumn 用于 ErrorLog 和 ChangeLog 表 - 这会导致从每个类中删除“Id”属性,这对于每个类型的表继承是正确的。但是,它也会导致 ErrorLog 和 ChangeLog 类被从生成的 DbContext 中删除,并且 ErrorLog 和 ChangeLog 类中出现以下注释:

// The table 'ChangeLog' is not usable by entity framework because it
// does not have a primary key. It is listed here for completeness.
  • 有没有办法不指定继承关系 导致生成器从模型中省略派生表?

  • 有没有办法阻止生成器包含导航 生成的 POCO 中的属性?

【问题讨论】:

  • 使用 UpdateColumn 解决 TPT 中重复 id 问题的重要指针。谢谢。

标签: sql-server entity-framework entity-framework-6 poco


【解决方案1】:

v2.27 中的最新版本(2017 年 1 月 5 日)修复了一个问题 (#167),其中隐藏列和主键现在可以正常工作。

如有任何问题,请在https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/issues/181 更新 GitHub 案例

【讨论】:

    【解决方案2】:

    【讨论】:

    • 非常感谢@ErikEJ - 一个非常有用的链接。我已经听从了建议,虽然我取得了一些进展,但我仍然遇到问题 - 即派生表正在生成 POCO,并附有 cmets 说它们“不能被实体框架使用,因为它们没有主键'。我在 GitHub 项目中提出了一个问题。
    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2018-04-27
    • 2011-06-30
    • 1970-01-01
    • 2010-10-08
    • 2016-06-02
    相关资源
    最近更新 更多