Entity Framework 7 combines table names with the first code approach

I am new to ASP / EF. I am using ASP 5 and Entity Framework 7 in my personal project.

Thus, I can create a database and tables with the first code approach, but all table names are unique and are not duplicated by default.

In my ServerMatrixDemoDB DbContext file that I created below DBSets:

public class ServerMatrixDemoDB : DbContext { public ServerMatrixDemoDB() { Database.EnsureCreated(); } public DbSet<Domain> Domains { get; set; } public DbSet<Environment> Environments { get; set; } public DbSet<Network> Networks { get; set; } public DbSet<OsVersion> OsVersions { get; set; } public DbSet<Tier> Tiers { get; set; } public DbSet<Service> Services { get; set; } public DbSet<HardwareType> HardwareTypes { get; set; } public DbSet<Powershell> Powershell { get; set; } public DbSet<DotNetVersion> DotNetVersions { get; set; } public DbSet<Status> Status { get; set; } public DbSet<Servers> Servers { get; set; } public DbSet<Application> Applications { get; set; } } 

And in my startup.cs file, I use the code below:

 public void ConfigureServices(IServiceCollection services) { services.AddMvc() // Add MVC Dependency. .AddJsonOptions( opt => { opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // Api convert all property names to CamelCase. } ); services.AddLogging(); // Enable Logging for database errors. services.AddEntityFramework() .AddSqlServer() .AddDbContext<ServerMatrixDemoDB>(options => { options.UseSqlServer(@"Server=.\SQLEXPRESS;user id=sa;password='passwrd';Database=ServerMatrixDemoDB;integrated security=True;"); }); services.AddTransient<ServerMatrixDemoSeedData>(); } 

As soon as I create and run a project, a database and tables are created, but all table names are unique. Is there a way to pluralize table names in the first code approach?

+7
c # entity-framework entity-framework-core
source share
2 answers

you can do this in an overload of OnModelCreating as -

 protected override void OnModelCreating(ModelBuilder modelBuilder) { foreach (var entity in modelBuilder.Model.GetEntityTypes()) { modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s"); } } 

you can also do this using "data annotations"

  [Table("blogs")] public class Blog { public int BlogId { get; set; } public string Url { get; set; } } 

or fluent api

 class MyContext : DbContext { public DbSet<Blog> Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>() .ToTable("blogs"); } } 

for more information see - Documentation for EF7

+8
source share

You can also use the PluralizationService and create an attribute that inherits from TableAttribute:

 /// <summary> /// Specifies the database table that a class is mapped to with pluralization. /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want users to be able to extend this class")] public class PluralizedTableAttribute : TableAttribute { private const string _englishCultureName = "en-us"; /// <summary> /// Initializes a new instance of the <see cref="PluralizedTableAttribute"/> class. /// </summary> /// <param name="name">The table name.</param> public PluralizedTableAttribute(string name) :base(PluralizationService.CreateService(CultureInfo.GetCultureInfo(_englishCultureName)).Pluralize(name)) // pluralization only suported in English. { } } 

And you will use it just like AtbleAttribute, except that everything will be luralized

-one
source share

All Articles