EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

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

Open ajcvickers opened this issue 6 years ago • 1 comments

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");
        }
    }
}

ajcvickers avatar Dec 07 '19 19:12 ajcvickers

Related: https://github.com/aspnet/EntityFramework.Docs/issues/1989

AndriySvyryd avatar Dec 14 '19 00:12 AndriySvyryd