【发布时间】:2014-08-12 11:57:09
【问题描述】:
我正在使用 Entity Framework 6 Code First,并且正在使用 Fluent API 配置我的域模型的映射。我看不到如何为表格创建导航属性,这有点棘手。 我有几个会产生噪音的物体,我想在 NoiseRecord 表中记录这些噪音。
我需要某种条件映射,比如:
modelBuilder.Entity<NoiseRecord>().HasRequired(n=>n.Origine.OrigineType()=="Car").WithMany(c=>c.NoiseRecords);
这将是 Car Navigation Property 的映射以避免这种情况,例如,它包括与飞机相关的记录。
这是我的代码
public interface INoisy
{
int ID {get; set;}
string OriginType()
...
//And other useful things not related to persistence
}
public class Car : INoisy
{
...
ICollection<NoiseRecord> NoiseRecords { get; set; }
string OrigineType()
{
return "Car";
}
}
public class Plane : INoisy
{
...
ICollection<NoiseRecord> NoiseRecords {get; set;}
string OrigineType()
{
return "Plane";
}
}
还有几个其他类也实现了 INoisy。 下面是 NoiseRecord 表。
public class NoiseRecord
{
public int RecordID {get; set;}
public INoisy NoiseOrigine {get; set;}
public double NoiseMagnitude {get; set;}
}
我正在寻找一种使用 Fluent API 实现这一目标的方法。
谢谢!
【问题讨论】:
标签: c# entity-framework ef-code-first fluent