Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:11:26+00:00 2026-06-06T17:11:26+00:00

I’m trying to configure a one to one relationship between entities which both have

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:11:28+00:00Added an answer on June 6, 2026 at 5:11 pm

    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 :

        private static void UpdatePrincipalTables(DbDatabaseMapping databaseMapping, DbTableMetadata toTable, bool removeFks, EdmAssociationType associationType, EdmEntityType et)
        {
            ...
        }
    

    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 :

    fk => fk.DependentColumns.SequenceEqual<DbTableColumnMetadata>(tableInfo.Value);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.