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.