可空"标导航属性"在EF 4.0:从一个单独的数据库表中的非可空列映射到一个可空标实体属性? [英] Nullable "scalar navigation properties" in EF 4.0: Mapping a non-nullable column from a separate database table to a nullable scalar entity property?
问题描述
使用实体框架4.0版本(或任何其他版本与.NET 4.0兼容),我要地图这个现有的关系型数据库模式:
这种逻辑对象模型:
我已经尝试设置如下:(我希望德国的标题不会太让人迷惑)
实体框架给了我这个错误:
错误3031:映射碎片问题...:非空列
FooBs.B
表中的FooBs
映射到一个可空实体属性。的
在逻辑模型, B
应该是空。然而,在数据库中,它是没有,因为它驻留在一个单独的表。 (我想避免null的数据库列。)它仅在 FOOS
和 FooBs
连接起来(由于成为可空1:0..1基数)
我怎么能解决我的映射,在不改变任何数据库模式或对象模型?
PS:我也试过这个EF 6.0 code第一映射:
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器) { modelBuilder.Entity<富>() .HasKey(F => f.Id) .Property(F => f.Id).HasColumnName(FooId)HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)。 modelBuilder.Entity<富>()地图(F => { f.Property(_ => _.A); f.ToTable(FOOS); })地图(F => { f.Property(_ => _.B); f.ToTable(FooBs); }); }
但是,这也不行:从数据库中读取数据时,EF会忽略其中有没有子记录中的所有记录
FooBs
;写入到数据库时,它试图插入NULL
到FooBs.B
所有富
具有其B
属性设置为空
。
目前应该是一个相当脏的解决方案。这将需要一些code更改,但会留下与现场的富
实体 A
和 B
。
富
类:
类Foo {
[键]
公众诠释FooId {获得;组; }
公众诠释A {获得;组; }
[NotMapped]
公众诠释? B {
得到 {
返回FooB == NULL?空:FooB.B;
}
组 {
如果(价值== NULL){
FooB = NULL;
} 其他 {
如果(FooB == NULL)
FooB =新FooB();
FooB.B =(INT)值;
}
公共虚拟FooB FooB {获得;组; }
}
和映射到数据库类 FooB
:
类FooB {
[键键,外键(FooId)
公众诠释FooId {获得;组; }
公众诠释B {获得;组; }
}
在旁注 - 这似乎是很奇怪的方式基本上是单一的空列添加到表,因为有其中 FooB
可以有一个以上的非无逻辑的方法-nullable列,这不会导致删除整个实体上设置列的值设置为null。
另一种选择是创建一个数据库视图,将行为像你想和映射到实体。
Using Entity Framework version 4.0 (or any other version that is compatible with .NET 4.0), I want to map this existing relational database schema:
to this logical object model:
which I have tried setting up as follows: (I hope the German captions won't be too disorienting.)
Entity Framework gives me this error:
Error 3031: Problem in mapping fragments …: Non-nullable column
FooBs.B
in tableFooBs
is mapped to a nullable entity property.
In the logical model, B
ought to be nullable. However, in the database, it isn't, because it resides in a separate table. (I like to avoid nullable database columns.) It only becomes nullable when Foos
and FooBs
are joined (due to the 1:0..1 cardinality).
How can I fix my mapping, without altering either the database schema or the object model?
P.S.: I also tried this EF 6.0 code-first mapping:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Foo>() .HasKey(f => f.Id) .Property(f => f.Id).HasColumnName("FooId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); modelBuilder.Entity<Foo>().Map(f => { f.Property(_ => _.A); f.ToTable("Foos"); }).Map(f => { f.Property(_ => _.B); f.ToTable("FooBs"); }); }
But this doesn't work either: When reading from the database, EF ignores all records for which there is no sub-record in
FooBs
; when writing to the database, it attempts to insertNULL
intoFooBs.B
for allFoo
that have theirB
property set tonull
.
There is a rather "dirty" solution that should work. It would require some code changing but would leave your Foo
entity with field A
and B
.
Foo
class:
class Foo {
[Key]
public int FooId { get; set; }
public int A { get; set; }
[NotMapped]
public int? B {
get {
return FooB == null ? null : FooB.B;
}
set {
if(value == null) {
FooB = null;
} else {
if(FooB == null)
FooB = new FooB();
FooB.B = (int)value;
}
public virtual FooB FooB{ get; set; }
}
And mapped to database class FooB
:
class FooB {
[Key, ForeignKey("FooId")]
public int FooId { get; set; }
public int B { get; set; }
}
On side note - it seems like very strange way to add essentially single nullable column to a table, as there is no logical way where FooB
could have more than one non-nullable column, that wouldn't result in deleting whole entity on setting columns value to null.
Another option is creating a database view that would behave like you want and map that to entity.
这篇关于可空&QUOT;标导航属性&QUOT;在EF 4.0:从一个单独的数据库表中的非可空列映射到一个可空标实体属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!