EF4 Code First make tables names singular

与世无争的帅哥 提交于 2019-11-30 14:52:46

问题


I'm using standards for singular table names. EF4 Code First has by default to pluralize table names. I have put the code to override this convention, but seems is not working.

using section:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Database;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions.Edm;

Data context:

public class SiteDataContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<BlogFeedback> BlogFeedbacks { get; set; }
        public DbSet<BlogCategory> BlogCategories { get; set; }

        // Twist our database
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
            base.OnModelCreating(modelBuilder);
        }
    }

Tables created:

  • Blogs
  • BlogFeedbacks
  • BlogCategories

When because the convention override (and what I need) should be:

  • Blog
  • BlogFeedback
  • BlogCategory

Anyone has an idea why the override line is not working? Thanks a lot.


回答1:


Your using the wrong convention. You need to do the below.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}



回答2:


I our current project we had the same problem. I wrote about it in another question, but here it is:

Entity Framework v4 and underscores in column names

It's a little more complex than expected.

HTH



来源:https://stackoverflow.com/questions/5898661/ef4-code-first-make-tables-names-singular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!