- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个系统,其中包含多个具有一对多关系(父子关系)的自引用实体。我想为所有这些实体使用公共(public)基类:
public class SelfReferencing
{
public SelfReferencing Parent {get; set;}
public ICollection<SelfReferencing> Children {get; set;}
}
并从 SelfReferencing 继承特定的实体。在尝试执行以下操作时,Fluent API 映射要求引用属性为定义类型:
modelBuilder.Entity<ConcreteSelfReferencing>()
.HasMany(e => e.Children)
.WithOptional(e => e.Parent);
那么,你能帮我找到一种利用继承并映射实体的可能性吗?
谢谢
最佳答案
Note: The example below is known as Table-Per-Hierarchy (TPH) - i.e. all contained in one table. Click on this link for Table-Per-Type (TPT), which has different tables for each type.
当使用基类型和继承类型时,您必须告诉 EF 如何确定特定继承类型的关联。
获取您的代码:
public abstract class SelfReferencing
{
public SelfReferencing Parent { get; set; }
public ICollection<SelfReferencing> Children { get; set; }
}
public class ConcreteSelfReferencing : SelfReferencing
{
}
EF 现在必须确定子类是 ConcreteSelfReferencing
还是任何其他类型的子类。这是由表本身的鉴别器确定的,到该表的列不是映射的一部分。
再举个例子,类似于我过去用过的:
public abstract class Policy
{
public int Id { get; set; }
public string PolicyNumber { get; set; }
}
public class InsurancePolicy : Policy
{
}
public class CarPolicy : Policy
{
}
表格的结构是这样的:
| Id | PolicyNumber | Type | ..... |
1 CAR0001 C
2 ISN0001 I
要让 EF 正确生成它们,您需要:
public class MyContext : DbContext
{
public MyContext() : base()
{
}
public DbSet<Policy> Policies { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
var policyMap = modelBuilder.Entity<Policy>();
// Set up discriminators
policyMap.Map<InsurancePolicy>(p => o.Requires("Type").HasValue("I"))
.Map<CarPolicy>(p => o.Requires("Type").HasValue("C"));
// Notice that `Type` is only used for the discriminators, not an actual
// mapped property
policyMap.HasKey(x=>x.Id);
policyMap.Property(x=>x.PolicyNumber);
}
}
从您的代码中,您可以自己进行过滤,或者将过滤放在 DbContext
中。这是来自单独类(class)的示例。
public class PolicyRepository
{
private MyContext context = new MyContext();
public PolicyRepository()
{
}
public IQueryable<InsurancePolicy> GetInsurancePolicies()
{
return this.context.Policies.OfType<InsurancePolicy>();
}
public IQueryable<CarPolicy> GetCarPolicies()
{
return this.context.Policies.OfType<CarPolicy>();
}
}
关于c# - Fluent API EF 继承和映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23997972/
这是我的 Fluent 模型 struct Ailment: PostgreSQLModel { enum Frequency: String , Content { case regular
我正在尝试使用 Fluent Nhibernate 自动映射一个简单的继承层次结构,并且我需要为每个表使用与其类稍有不同的名称(下划线而不是 Pascal 大小写)。这似乎是一个使用约定的明显地方。我
如何为没有标识列的表指定流畅的 NHibernate 映射? 我想要这样的东西: public sealed class CustomerNewMap : ClassMap, IMap { p
使用 FluentMigrator,有没有办法找出 MigrateUp() 函数是否确实会迁移某些东西,或者它是否已经是最新的? 最佳答案 没有简单的方法可以使用公共(public) api 判断 M
我正在使用 Fluent NHibernate,我喜欢它! 我有一个小问题:启动时间大约是 10 秒,我不知道如何优化 Fluent nHibernate 为了减少这个启动时间的问题,我把它放在一个线
我在 Fluent NHIbernate 中使用 AutoPersistenceModel 来映射我的所有实体,并且一切正常:D 但是,我的几个对象有 public virtual IList Com
我有一个数据库,我正在运行多个应用程序。我喜欢通过为每个应用程序创建模式来分隔表。对于我最新的应用程序,我使用的是 FluentNHibernate。似乎我的大部分管道都是正确的,但是当我尝试查询其中
应用程序有许多扩展程序集,它们包含其类的映射。我需要为这些映射中的所有(基本、连接、多对多等)表名添加前缀。 例如 Assembly: ~/bin/Extensions/Foo.dll
您好,我很好奇 DDD 是如何使用 Fluent Nhibernate 真正实现的。例如,我有一个名为 User 的实体类和另一个名为 UserProfile 的类,就我而言,UserProfile
是否可以在 Fluent NHibernate 中映射来自多个程序集的实体? 我试过了 AutoPersistenceModel .MapEntitiesFromAssemblyOf() .AddEn
我有一个看起来像这样的基类: public abstract class MyBaseClass { public virtual DateTime UpdatedOn { get; set;
我有 Post 和 Comment 类,它们有一对多的关系,其中 Post 有一个评论列表。我如何将其映射为与 Fluent NHibernate 的单向关系,因为评论不需要知道其父 Post?目前,
我对如何查询模型对象的子对象并立即使用它感到困惑。我的Client包含数量Station child final class Client: PostgreSQLModel { var sta
目前我有一个表“ComponentAnalysis”和一个表“HistoryOfUse”,我正试图在 Fluent NHibernate 中进行映射。 一个成分分析应该只有1个使用历史,一个使用历史应
正如标题所说,我想知道我是否应该避免将 fluent nhibernate 用于生产代码,或者它是否足够成熟,可以“深入研究”? :) 最佳答案 FluentNHibernate API 尚未稳定下来
我正在尝试使用 Fluent NHibernate,我有几个问题。我发现缺少文档。 我知道 Fluent NHibernate/NHibernate 允许您自动生成数据库模式。人们通常只对测试/开发数
我正在使用 fluent-nhibernate 约定来映射我的实体: public class HasManyConvention : IHasManyConvention {
如何更改多列索引中的列顺序? IE: mapping.References(x => x.SomeReference).SetAttribute("index", "IX_index"); mappi
我需要像下面的代码一样创建一个外键: Create.ForeignKey().FromTable("TCGDocFiscalOpMedItem").ForeignColumn("IDCabecalho
我正在使用 Sharp 架构,并且在许多情况下都在实体中使用了值对象。这是一个明显的简单示例: public class Person : Entity { protected Person(
我是一名优秀的程序员,十分优秀!