【发布时间】:2015-06-01 09:42:23
【问题描述】:
我有一个关系表,但有一个额外的列Active bit not null
我不明白我应该如何在 EF6 中映射,我有 3 个表,Foo、Bar 和 FooBar,其中 FooBar 是关系表
FooBar {
FooId: int (Key and FK)
BarId: int (Key and FK)
Active: bit
}
Foo 实体
public class Foo
{
public int Id { get;set; }
public ICollection<FooBar> Bars { get; set; }
...
}
FooBar 实体
public class FooBar
{
public Foo Foo { get;set; }
public Bar Bar { get;set; }
public bool Active { get;set; }
}
现在到问题,EF 配置 FooBar 配置
public class FooBarConfiguration : EntityTypeConfiguration<FooBar>
{
public FooBarConfiguration()
{
HasKey(fb => new[] {fb.Foo.Id, pv.Bar.Id}); //Is this correct?
Property(pv => pv.Active);
ToTable("ProductVehicle");
}
}
我不知道 Foo 配置应该是这样的,所以我卡住了,尝试了类似的东西
public class FooConfiguration : EntityTypeConfiguration<Foo>
{
public FooConfiguration()
{
HasKey(f => f.Id);
HasMany(f => f.Bars)
.WithRequired(f => p.Foo)
.HasForeignKey(f => f.Foo.Id);
ToTable("Foo");
}
}
我明白了
“System.InvalidOperationException”类型的第一次机会异常 发生在EntityFramework.dll中
附加信息:属性表达式 'f => f.Foo.Id' 无效。表达式应该代表一个属性:C#: 't => t.MyProperty' VB.Net:'函数(t)t.MyProperty'。指定时 多个属性使用匿名类型:C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'。
我也改变了 FooConfig
HasMany(f => p.Bars)
.WithRequired(fb => fb.Foo)
.Map(map => map.MapKey("FooId"));
我得到了一点时间,现在它在 FooBar 配置上失败了
“System.InvalidOperationException”类型的第一次机会异常 发生在EntityFramework.dll中
附加信息:属性表达式 'fb => new [] {fb.Foo.Id, fb.Bar.Id}' 无效。表达式应该 表示一个属性: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'。指定多个属性时使用匿名 类型:C#:'t => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.
编辑:我最终得到了这个解决方案,让它更受域驱动,
public class ProductVehicle
{
private Product _product;
private Vehicle _vehicle;
internal int ProductId { get; set; }
internal int VehicleId { get; set; }
public Product Product
{
get { return _product; }
set
{
_product = value;
ProductId = value.Id;
}
}
public Vehicle Vehicle
{
get { return _vehicle; }
set
{
_vehicle = value;
VehicleId = value.Id;
}
}
public bool Active { get; set; }
}
【问题讨论】:
标签: c# entity-framework entity-framework-6