I have an entity type MyEntity
that has a primary key string MyEntityCode
I want to make a second entity MyEntityInfo
that are extended properties that some MyEntity
's are logically associated.
That makes the relationship between these entities one-to-one, with one end optional -- MyEntity
logically optionally has a MyEntityInfo
, without a navigation property, and MyEntityInfo
is required to have a single MyEntity
(with a navigation property).
I want to encode this in SQL as MyEntityInfo
having a primary key BaseEntityCode
that's also a foreign key to MyEntity
's MyEntityCode
.
How do I configure this encoding in EF6 fluent configuration API.
Sample code
public class MyEntity {
public string MyEntityCode {get; set;}
public int SomeProperty {get; set;}
}
public class MyEntityInfo {
public MyEntity BaseEntity {get; set;}
public string BaseEntityCode {get; set;}
public int OtherInfo {get; set;}
}
public MyEntityConfiguration : EntityConfiguration<MyEntity> {
public MyEntityConfiguration(){
HasKey(e => e.MyEntityCode);
}
}
I thought I could configure MyEntityInfo
as
public MyEntityInfoConfiguration : EntityConfiguration<MyEntityInfo> {
public MyEntityInfoConfiguration(){
HasKey(e => e.BaseEntityCode);
HasRequired(e => e.BaseEntity).WithOptional().WithForeignKey(e => BaseEntityCode);
}
}
but WithOptional()
doesn't allow chaining to WithForeignKey
Doing the same but with WithMany()
so that a foreign key is possible, the multiplicity constraint of one is violated:
Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.