EF4 Code First 使表名称为单数
我正在使用单数表名称的标准。默认情况下,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用了错误的约定。您需要执行以下操作。
Your using the wrong convention. You need to do the below.
在我们当前的项目中,我们遇到了同样的问题。我在另一个问题中写过它,但这里是:
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