我可以使用 Entity Framework 4 CTP5 访问 TPH 映射中的鉴别器值吗
使用 Entity Framework 4 CTP5 Code First 和 此示例
是否可以访问鉴别器值?
我想在类似
context.BillingDetails.Select(x => new { Number = x.Number, DiscrimitatorValue = /* how do I get the discriminator value? */ });
From 这篇文章 我知道鉴别器无法映射到属性,但是还有其他方法可以访问它吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我可能在这方面迟到了,但我只是向基类添加了一个 getter 属性,该属性返回当前类型的名称:
由于默认情况下 EF 将为 Discriminator 字段使用相同的值,因此它们将匹配向上。
I may be late to the game on this one, but I just added a getter property to the base class that returned the name of the current type:
Since by default EF is going to use this same value for the Discriminator field, they will match up.
在 EF Core 2.1 中(我没有检查以前的版本),将
Discriminator
添加到抽象基类作为私有集属性就足够了。它将被映射为具有足够的值。EF 本身会自动将适当的鉴别器值插入到数据库中,并在读取时自动将其设置为对象。
In EF Core 2.1 (I haven't checked previous versions) it's enough to add
Discriminator
to the base abstract class as private set property. It will be mapped with adequate value.EF by itself will automatically insert appropriate discriminator value to the database and will automatically set it to an object on read.
从Morteza Manavi 在他的帖子的评论中简单的答案是否定的
要访问鉴别器,我必须对数据库执行
SqlQuery
或更改我的映射策略。After further information from Morteza Manavi in the comments of his post the simple answer is no
To access the discriminator I would have to execute a
SqlQuery
against the database or change my mapping strategy.撇开原因不谈,我最近遇到了同样的问题,但相信这仍然与 相关EF 框架 v4。
首先,创建一个视图,将鉴别器值选择为两列。
其次,在上下文创建期间将视图映射到实体:
第三,使用视图中的一列为派生类定义鉴别器映射:
最后,使用视图中的另一个鉴别器列定义一个 getter 和一个私有 setter
DatabaseGenerate
注释设置为Compulated
以防止 EF 更新/插入此字段:您可以更改要保护的私有 setter,并在派生的构造过程中显式设置此值实体,以便鉴别器在持久化之前具有值:
Reason aside, I recently ran into the same problem but believe this is still relevant for v4 of the EF Framework.
First, create a view which selects the discriminator value into two columns.
Secondly, map the view to your entity during context creation:
Thirdly, define your discriminator mapping for your derived class using one of the columns in your view:
Finally, define a getter and a private setter for the other discriminator column in your view with the
DatabaseGenerated
annotation set asComputed
to prevent EF from updating/inserting for this field:You can change the private setter to be protected and set this value explicitly during the construction of your derived entities so that the discriminator has a value prior to being persisted:
要扩展 @Michael Black 对 Entity Framework Core 2.1 的回答(之前?在 2.1.4 中测试过),
您可以使用您想要的任何属性名称、数据库字段名称和数据类型。
创建一个属性:
然后在您的上下文类中通过 modelBuilder 使用 Fluent API:
如果您需要构建可组合查询(例如,在鉴别器上进行分组等),这非常有用。
To expand on @Michael Black's answer for Entity Framework Core 2.1 (earlier? tested in 2.1.4)
You can use any property name, database field name and data type you want.
Create a property:
Then in your context class with the fluent API via modelBuilder:
This is really useful if you need to build composable queries that e.g., group on the discriminator, etc.
为什么不使用下面的查询呢?
Why don't you use the following query instead?
您可以使用您为 EF Core 中的鉴别器指定的名称添加属性。示例:
在 DBContext 中:
在基类中执行以下操作:
You can add a property with the name you gave to the discriminator in EF Core. Example:
In DBContext:
In base class do:
对我有用的是按照以下步骤通过
.HasCompulatedColumnSql()
检索鉴别器列:公共字符串EntityType{获取;放; }
entity.Property(e => e.EntityType).HasCompulatedColumnSql("dbo.GetEntityType([Id])")
What worked for me was retrieving the Discriminator column via
.HasComputedColumnSql()
following the steps below:public string EntityType{ get; set; }
entity.Property(e => e.EntityType).HasComputedColumnSql("dbo.GetEntityType([Id])")