Here's part of the setup. Names are simplified: I have following classes:
- MyClass
- MySubclass1 : MyClass
- MySubClass2 : MyClass
MyClass
is part of another class (and table), which I will leave out.
I'm trying to do an EF6 query (using context.Set<MyClass>.Where(...)
)
I want to return the subclasses instead of the parent class and I want to avoid using the Discriminator
. The reason is: there is a property on MyClass that already defines whether it's MySubclass1
or MySubclass2
.
The property is called KindId
and is an FK
to a lookup table (Kinds
).
The EF-part is being autogenerated as follows:
public partial class MyClassMap : EntityTypeConfiguration<MyClass>
{
public MyClassMap ()
{
ToTable("MyClasses", "MySchema");
//...etc (other implementations)
HasRequired(c=> c.Kind)
.WithMany(kind => kind.MyClasses)
.HasForeignKey(c => new { c.KindId });
Initialize();
}
partial void Initialize();
}
This means, I can only update Initialize(). I have done so as follows:
public partial class MyClassMap
{
partial void Initialize()
{
Map<MySubclass1>(c => c.Requires("KindId").HasValue(1));
Map<MySubclass2>(c => c.Requires("KindId").HasValue(2));
}
}
When I run the EF query, I'm getting following exception:
error 3023: Problem in mapping fragments starting at lines x:Column MyClass.KindId has no default value and is not nullable. A column value is required to store entity data.
I tried setting the default value on the DB, but I think it requires the Default to be set within EF. Question is: how do I do this using the FluentApi?