Unable to find multiple table shape

For some reason, the first EF7 code (vNext) will not use / find the plural form of my table. I tried to add a table attribute to the model, but this does not solve the problem.

[Table("Units")]
public class Unit

If I call the Unit table, then there is no problem. If I name the table units, then it is not found.

What am I doing wrong or missing?

Thank.

+4
source share
5 answers

Here is how I decided:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Unit>().ToTable("Units");
}
+3
source

For Entity Framework 7 beta1, I solved this problem as follows:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Unit>().ForRelational(rb =>
    {
        rb.Table("Units");
    });
}
+3
source

Entity Framework 7 Fluent API. , , EF 6 EF7.

public static class ModelBuilderExtensions
{
    public static void PluralizeNames(this ModelBuilder modelBuilder)
    {
        var types = modelBuilder.Model.EntityTypes;

        foreach (var type in types.Where(type => type.ClrType != null))
        {
            modelBuilder.Entity(type.ClrType)
                        .ForRelational()
                        .Table(type.ClrType.Name.Split('`')[0].Pluralize());
        };
    }
}

.Pluralize(). Humanizer, , , . ( https://github.com/srkirkland/Inflector/blob/master/Inflector/Inflector.cs , DNX.)

.Split() type.ClrType.Name, , IdentityUserRole`1.

DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.PluralizeNames();
}

Ps; it works for me

+1
source

Now ToTable and ForRelational are both missing in beta5 EF7. So I used the code below.

       protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<Role>().ForSqlServer().Table("Role");
        }
0
source

You need to add "Microsoft.EntityFrameworkCore.Relational" to your project.json and restore your package. The .NET kernel is broken into small pieces for less memory. Therefore, you need to explicitly indicate what you want.

0
source

All Articles