EF4 Code First 使表名称为单数

发布于 2024-11-04 23:38:57 字数 1175 浏览 0 评论 0原文

我正在使用单数表名称的标准。默认情况下,EF4 Code First 具有复数表名称。我已经编写了代码来覆盖此约定,但似乎不起作用。

使用部分:

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;

数据上下文:

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

创建的表:

  • 博客
  • BlogFeedbacks
  • BlogCategories

当因为约定覆盖(以及我需要的)应该是:

  • Blog
  • BlogFeedback
  • BlogCategory

任何人都知道为什么覆盖线不起作用?多谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-11-11 23:38:58

您使用了错误的约定。您需要执行以下操作。

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

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
陈独秀 2024-11-11 23:38:58

在我们当前的项目中,我们遇到了同样的问题。我在另一个问题中写过它,但这里是:

Entity Framework v4 和列名称中的下划线

它比预期的要复杂一些。

华泰

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文