Entity Framework – (复数)Plural and (单数)Singular 表名Table names
By default, the Entity Framework will assume that all of the names of your tables in your database are either pluralised(复数形式的), or in the case of code first, you would like them to be pluralised when created.
E.g. you have a table called “Product” and not “Products”, or you want your table to be called “Product” and not “Products”
This is the problem that I had. My MVC application consisted of one web page that just dumped out the contents of the “Product” table onto the page. When I browsed to the page, I got an “Invalid object name ‘dbo.Products’.” yellow screen of death runtime error.
The Solutions
1. Rename the table to “Products”. I didn’t want to do this as I’m from the school of singular table names. I was also curious about situations where the tables couldn’t be renamed.
2. Make use of Entity Framework’s fantastic Conventions, that allow you to specify how you have or want your database to be setup.
To tell Entity Framework not to pluralise database table names, simply add the following code into your DbContext class:
public class EfDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
This code will remove the Pluralising convention that is by default attached to all model builders. You will then be able to access database tables with Singular names
之所以这样,是因为开发者就表名是否使用复数没有达成一致。这个教程使用了单数形式,但重点是您可以自己选择使用哪种形式来命名。
参考:http://edspencer.me.uk/2012/03/13/entity-framework-plural-and-singular-table-names/
Entity Framework – (复数)Plural and (单数)Singular 表名Table names的更多相关文章
- Entity Framework 重写OnModelCreating,控制生成表名的单复数
重写OnModelCreating,控制生成表名的单复数 public class MYDbContext : DbContext { public DbSet<User> Users { ...
- Entity Framework - Func引起的数据库全表查询
原文:http://www.cnblogs.com/dudu/archive/2012/04/01/enitity_framework_func.html 使用 Entity Framework 最要 ...
- 用Entity Framework 来创建MySql数据库和表结构
1. 创建VS工程 2.添加新项, 选中ADO.Net Entity Data Model模板 3.填入Host及数据库名字, 如果没有此数据库, 会提示创建 4.添加edmx后, 右击选择属性,配置 ...
- Entity Framework Code-First(9.7):DataAnnotations - Table Attribute
DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...
- 《Entity Framework 6 Recipes》中文翻译系列 (39) ------ 第七章 使用对象服务之配置模型和使用单复数服务
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-3 配置模型 问题 你想了解配置模型中的各种选项. 解决方案 当你添加一个AD ...
- Code First :使用Entity. Framework编程(5) ----转发 收藏
第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...
- MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型
为ASP.NET MVC程序创建Entity Framework数据模型 (1 of 10) By Tom Dykstra |July 30, 2013 Translated by litdwg ...
- 【译著】Code First :使用Entity. Framework编程(5)
第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...
- Entity Framework Code First关系映射约定
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
随机推荐
- Chapter 2: 随机变量
1. 随机变量, 离散型随机变量,连续型随机变量 设$\Omega$为随机试验的样本空间,$X:\Omega \rightarrow R$是定义在样本空间$\Omega$上的实值函数,则称$X$为随机 ...
- treetable 前台 累计计算树值 提交后台
treetable 累计计算树值 效果图 html 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- android HAL 教程(含实例)
http://www.cnblogs.com/armlinux/archive/2012/01/14/2396768.html Android Hal 分析 ...
- python学习之深入
一.迭代器和生成器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. ...
- get传中文参数乱码解决方法
通常我们前端不同页面之间传参数用得最多的方法就是get方法:在url后面加上参数.例如:www.test.com?id=1&name=hello. 英文和字母很好处理,但是如果有的参数值为中文 ...
- solr 3.5.0 与 tomcat 7.0.5 整合配置
1.将D:\software\lucene-3.6.0\apache-solr-3.5.0\example\solr下的文件拷贝到要作为服务器的位置(如:D:\lucene\solr\home),可以 ...
- jQuery.is() 函数
is() 函数 判断当前对象是否符合指定表达式 语法 $selector.is(表达式)//指定表达式 返回值 返回值为布尔型(true/false) 当当前对象包含多个元素时,只要任意元素满足指定表 ...
- iOS - Card Identification 银行卡号识别
1.CardIO 识别 框架 GitHub 下载地址 配置 1.把框架整个拉进自己的工程,然后在 TARGETS => Build Phases => Link Binary With L ...
- 在VIM中进行快速的查找和替换
VIM是被誉为非常高效的文本编辑软件.但是掌握并高效的使用是件有难度的事情.在VIM中进行快速的查找和替换是提高VIM使用效率的重要方法.下面是我在阅读VIM用户手册时整理的一些资料: 行内搜索. f ...
- SQL与Mongodb聚合的对应关系(举例说明)
SQL中的聚合函数和Mongodb中的管道相互对应的关系: WHERE $match GROUP BY $group HAVING $match SELECT $project ORDER BY $s ...