Skip to content

Document how to compensate for conventions different in EF Core than EF6 #1971

Open

Description

@ajcvickers

For example, shadow FK property names in EF Core are PrincipalId rather than the Principal_Id name used by independent associations in EF6. This can be fixed with something like:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // Other model building here...
    
    foreach (var foreignKey in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetDeclaredForeignKeys()))
    {
        // Note that something different would be needed for composite keys
        if (foreignKey.Properties.Count == 1)
        {
            foreignKey.Properties[0].SetColumnName(foreignKey.PrincipalEntityType.Name + "_Id");
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions