问题标签 [tph]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
930 浏览

entity-framework - EF6 模型优先和 TPH 继承映射策略

我将 VS 2013 Premium 与 EF 6 一起使用,并希望将 Model First 与每个层次结构的表一起使用。我一直在寻找 2 个多小时的解决方案,但无济于事。

在模型设计器中,我只有TPT 作为选择。TPH 似乎在 VS 2010 的“Entity Designer Database Generation Power Pack”中,但我读到这在 2012 年不起作用,所以我认为它在 2013 年也不起作用。我看到对于 Code First TPH 是默认继承策略,那么使用 TPH 将 Code First 项目转换为 Model First 的答案是什么?Model First 和 TPH 还能做到吗?我有哪些选择?

0 投票
1 回答
1358 浏览

c# - 派生属性到特定表特定列的 EF6 TPH 映射

将带有 Customer 实体的旧数据库拆分为 3 个表 (1-1),并使用共享键。想要使用 Code First TPH 并将其映射到拆分表。这是简化的类层次结构(省略了许多原始属性及其映射):

以下是我尝试将其映射到 3 个表的方法:

但我不知道如何手动将 PrivateName 和 CompanyName 映射到所需表中的所需列。那可能吗?谢谢。

0 投票
1 回答
823 浏览

entity-framework - TPH 鉴别器不在基类/表上

我在使用 Entity Framework v6.1.1 继承 TPH 时遇到鉴别器问题

我希望鉴别器应该落在代表类的表上。但是,似乎 EF 正在尝试将鉴别器列映射到映射到派生类的表

例如

我也在使用现有的模式 - 即:

但是,当我尝试添加新员工时,遇到以下异常:

'System.Data.SqlClient.SqlException:无法将值 NULL 插入到列 'PersonTypeId'、表 'MyDb.dbo.Person' 中;列不允许空值。插入失败。该语句已终止'

它似乎正在尝试将鉴别器(错误地)插入派生表。希望有人可以提供帮助。

0 投票
0 回答
148 浏览

entity-framework - 子类的 TPH 简单配置/鉴别器问题

我将 Code first EF 6.1 与 DotConnect 一起用于 Oracle 数据提供程序和 TPH。我创建了一个有 2 个子类的类。我正在尝试将记录添加到现有的数据库表中。但是当我尝试保存更改时它会引发 DbUpdateException。

异常:{“ORA-06550:第 4 行,第 298 列:\nPL/SQL:ORA-00904:\“鉴别器\”:无效标识符\nORA-06550:第 4 行,第 1 列:\nPL/SQL:忽略 SQL 语句"}

这是我的代码:

Person.cs :-----------------

PersonEntityModel.cs --------------------

如果没有最后 2 行,则会引发异常:{"ORA-06550: line 4, column 298:\nPL/SQL: ORA-00904: \"Discriminator\": invalid identifier\nORA-06550: line 4, column 1: \nPL/SQL: SQL 语句被忽略"}

对于最后 2 行,它会引发此异常: {"ORA-06550: line 4, column 298:\nPL/SQL: ORA-00904: \"TYPE\": invalid identifier\nORA-06550: line 4, column 1: \nPL/SQL: SQL 语句被忽略"}

我是 TPH 的新手。如何解决以上问题?

另外,请给我发一些关于 TPH 的好文章。我已阅读此http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

感谢你的帮助,

0 投票
1 回答
1083 浏览

c# - 使用 TPH 时查询以包含鉴别器值

使用 TPH 时,我有不同的类型从基础继承。我们有一个跨抽象类型查询的搜索。在结果中,我们要显示类型:

IE。抽象车辆

汽车:车辆卡车:车辆

在结果中,我想显示“类型” - 即。“汽车”和“卡车”。

我试图使用 GetType().Name 但这失败了。

这是示例:

0 投票
1 回答
473 浏览

entity-framework - 实体框架 - 具有空数据库字段的 TPH 继承

我有 2 个继承自同一个基类的子类。它们都映射回同一张表。如果 db 表中的 field1 为 null 而 field2 不为 null,则它是一个子类。如果 field1 不为 null 而 field 2 为 null,则它是另一个子类。

我不断收到错误消息。实际消息是:“列名‘鉴别器’无效。” 它实际上说的是鉴别器......我没有把它作为一个通用术语。

这是我的代码示例:

如何正确映射?

0 投票
0 回答
425 浏览

c# - 实体框架tph映射属性多条件

我正在开发一个实现实体框架 6 代码优先模式的 Web 应用程序。在我的项目中,由于某些原因,我已经使用 TPH、TPT 和 TPC 模式完成了表继承,但现在我遇到了这样的问题:

1)我创建了一个带有一些属性的基表表 1,其中一些是用于识别继承类型的“关键”列

我必须在独立实体的一些属性中实现这个基表,比如这个例子:

理想的实现是我可以为所有实体使用相同的类(实体)和表,并且在映射中我会指定,例如,Table1Ref1 列将获取 TableName 值为“OtherEntity”和 ColumnName 值为“Ref1”和 ItemId 的记录在实体键值处;列 Table1Ref2 具有相同的 TableName 和 ItemId 和 ColumnName 在值 'Ref2' 等..

是否可以实施此解决方案?

0 投票
0 回答
111 浏览

asp.net-mvc - Can I and should I create one or many views and controllesr for TPH inheritance hierarchy in ASP.NET-MVC

I've read plenty of articles about inheritance with Entity Framework(in ASP.NET-MVC context too), everybody writes about database's side of the problem, but nobody brings up view side of the problem.

We got model classes:

and in the dbContext we hold:

Additional Question: Shoud I keep also public DbSet<Employee> Employees { get; set; } in dbContext?

Now if I click on Controllers directory right-click then Add Controller like this:

enter image description here

The outcome is getting the controller with Index,Details,Create,Delete,Edit actions for Person class.

All the Persons including those which are Employees will be shown on the Index.cshtml list. In the Details.cshtml view Employee's Salary will not be shown. In Edit.cshtml view Employee's Salary can't be edited(it is not visible). Furthermore POST Edit action cannot(can it?) bind Salary depending did Person xor Employee was edited. This could've been predicted, it is kind of obvious, but how to solve this?

Main question:

Should I create one controller for each type Person and Employee or only one for them togheter?

Should I create one Details,Create,Delete,Edit view for each type or only one and somehow check inside those views what type of object I want to present/edit/create in view? If I suppose to have one controller how to solve bindings in POST Edit action method?

Als if there is going to be one Index page with list consisting both Person, Employee objects how to check in the view what type of object is in the list when navigating to right Details,Create,Delete,Edit views?

Examplatory solution which comes to my mind: Create two controllers PersonController, EmployeeController with all the views, besides Index view and action for EmployeeController. Then basing on type of object in the list in index.cshtml lead to different views.

0 投票
0 回答
203 浏览

sql-server - 从实体框架 6 中的 TPH 基类派生的 TPT 实体?

我有一个使用 EF6 数据库的项目,该项目首先映射到 SQL 数据库。这是全新的,所以我控制 EF 模型以及数据库模式。

我目前有一张桌子,为了简单起见,我将其称为 Vehicle。我使用鉴别器列来获取子类实体汽车和卡车。这一切都很好。

现在我需要执行“软删除”并将所有已删除的车辆移动到 VehicleHistory 表中。(在尝试使用 EF 之后,我可能会使用 SQL 事务)。这需要可审查,因此我也需要映射此历史表,但我想将其保留在继承层次结构中,以便在其他类中轻松重用。

我的想法是为共享列创建带有 FK 到 Vehicle 的“vehiclecurrent”和“vehiclehistory”表。然后我会在 EF 中使用 TPT 来获取“carcurrent”、“carhistory”等...源自我的 TPH 类(例如,carhistory->car->vehicle)。这不起作用,我收到错误 3034:“具有不同键的实体映射到同一行”

所以我的问题基本上是我怎样才能做到这一点?这种方法会奏效吗?如何,或者有其他方法可以实现吗?谢谢!

0 投票
1 回答
252 浏览

c# - TPH 和多对多关系

嗨,我正在使用 TPH 继承来建立类似的可配置项。我的 TPH 模型是:

然后,我需要使用订单实体与子实体创建多对多和 1、一对多/多对一关系:

  • Order 有很多 WindowTypes
  • Order 有很多 WallType
  • 订单有很多屋顶类型
  • 订单有一个 RoofSize
  • 订单有一个 DoorSize
  • 订单有一个 GardenSize

但是,这不会为 RoofTypes、WallTypes 等生成多对多关系......我曾假设公共虚拟 ICollection Orders{get;set;} 将被继承。我如何能够建立与 order 的多对多和一对多关系的上述继承类?