Efcore: Can we change the fluent api in DbContext.OnModelCreating to Dictionary? This makes it easy to get the mapping of field types.

Created on 24 Aug 2019  路  2Comments  路  Source: dotnet/efcore

Such as:
public static Dictionary Mapping = new Dictionary
{
{ "Project.PrimaryKey", "id" },
{ "Project", "project" },
{ "Project.Id", "id" },
{ "Project.Name", "name" },
{ "Project.CreateTime", "create_time" },
{ "Project.UpdateTime", "update_time" },
{ "Project.Description", "description" },
{ "Project.Sort", "sort" },
.......
}

closed-question customer-reported

Most helpful comment

@188867052 You can create sugar methods as necessary:

```C#
private static void ConfigureNames(EntityTypeBuilder eb, Dictionary names)
{
foreach (var nameTuple in names)
{
eb.Property(nameTuple.Key).HasColumnName(nameTuple.Value);
}
}


```C#
modelBuilder.Entity<Project>(
    eb =>
    {
        ConfigureNames(eb, new Dictionary<string, string>
        {
            { "Id", "id" },
            { "Name", "name" },
            { "CreateTime", "create_time" },
            { "UpdateTime", "update_time" },
            { "Description", "description" },
            { "Sort", "sort" }
        });
    });

All 2 comments

@188867052 You can create sugar methods as necessary:

```C#
private static void ConfigureNames(EntityTypeBuilder eb, Dictionary names)
{
foreach (var nameTuple in names)
{
eb.Property(nameTuple.Key).HasColumnName(nameTuple.Value);
}
}


```C#
modelBuilder.Entity<Project>(
    eb =>
    {
        ConfigureNames(eb, new Dictionary<string, string>
        {
            { "Id", "id" },
            { "Name", "name" },
            { "CreateTime", "create_time" },
            { "UpdateTime", "update_time" },
            { "Description", "description" },
            { "Sort", "sort" }
        });
    });

Thank you, this is a good suggestion.

Was this page helpful?
0 / 5 - 0 ratings