I’m trying to configure a one to one relationship between entities which both have an inheritance hierarchy.
For the sample, let’s considered the following first inheritance chain :
[Table("A")]
public abstract class A
{
public Guid ID { get; set; }
...
}
[Table("AA")]
public class AA : A
{
...
}
[Table("AB")]
public class AB : A
{
...
}
Then, considered this second inheritance chain :
[Table("B")]
public abstract class B
{
public Guid ID { get; set; }
}
[Table("BA")]
public class BA : B
{
...
}
[Table("BB")]
public class BB : B
{
...
}
Add a one to one relationship between AA and BA with AA as principal entity :
[Table("AA")]
public class AA : A
{
...
public BA BAChild { get; set; }
...
}
[Table("BA")]
public class BA : B
{
...
public AA Parent { get; set; }
...
}
public class AAConfiguration : EntityTypeConfiguration<AA>
{
public AAConfiguration()
{
this.HasRequired(o => o.BAChild)
.WithRequiredPrincipal(o => o.Parent);
}
}
Add a one to one relationship between AB and BB with AB as principal entity :
[Table("AB")]
public class AB : A
{
...
public BB BBChild { get; set; }
...
}
[Table("BB")]
public class BB : B
{
...
public AB Parent { get; set; }
...
}
public class ABConfiguration : EntityTypeConfiguration<AB>
{
public ABConfiguration()
{
this.HasRequired(o => o.BBChild)
.WithRequiredPrincipal(o => o.Parent);
}
}
I also want that EF generates tables for entities A and B so I have added and registered the following empty EntityTypeConfiguration :
public class AConfiguration : EntityTypeConfiguration<A>
{
}
public class BConfiguration : EntityTypeConfiguration<B>
{
}
If you run the code like this you will get a bug during index creation (saw Unhandled Exception after Upgrading to Entity Framework 4.3.1)
So let’s do some tricky code and register a custom MigrationSqlGenerator derived from SqlServerMigrationSqlGenerator to avoid index creation based on my business naming rule :
public class Configuration : DbMigrationsConfiguration<DataContext>
{
public Configuration()
{
...
this.SetSqlGenerator("System.Data.SqlClient", new CustomSqlServerGenerator());
...
}
}
public class CustomSqlServerGenerator : SqlServerMigrationSqlGenerator
{
protected override void Generate(CreateIndexOperation createIndexOperation)
{
if (createIndexOperation.Columns.Count() == 1 && createIndexOperation.Columns.Any(o => o == "ID"))
return;
base.Generate(createIndexOperation);
}
}
public class DataContext : DbContext
{
static DataContext()
{
Database.SetInitializer<DataContext>(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
}
...
}
So now it’s time to generate the database, to make it, I use the following code :
...
DataContext dataContext = new DataContext();
dataContext.Database.Initialize(true);
...
And now if you look at the generated database you will saw that both table BA and BB has a foreign key for the table AB and that there is no foreign key between BA and AA !?!
I am probably missing something but I can’t see what’s wrong with this sample.
What can be done to generate the database properly ?
After doing some deep search throughout reflector it seems that this behavior is an EntityFramework bug.
I’ve located the problem in System.Data.Entity.ModelConfiguration.Configuration.Mapping.ForeignKeyPrimitiveOperations in the following function :
When EntityFramework loads the model, the DbDatabaseMapping instance is loaded with the base types and the foreign key constraints are added to the base types metadatas.
EntityFramework also store an instance of EdmAssociationType in the annotations of the foreign key constraint to maintain all the required informations to generate the correct constraint.
Latter, when the derived types are added and configured in the DbDatabaseMapping instance, EntityFramework tries to update the related end of the associations to move the foreign key constraints on the derived tables.
To identify the foreign key constraints to update the following selector is used where tableInfo.Value is an IEnumerable containing the dependent columns for the foreign key :
Unfortunately, in my case for the relation between AA and BA and for the relation between AB and BB the column “ID” is used.
So, both of them are updated a first time to replace the principal table metadata by AA metadata and a second time by AB metadata.
As a consequence, both of the foreign key are generated with AB as principal table !
I think that the EdmEntityType passed in the function parameters should be used in the selector to be compared to the entity type of the relationship end stored in the foreign key constraint annotations.