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的更多相关文章

  1. Entity Framework 重写OnModelCreating,控制生成表名的单复数

    重写OnModelCreating,控制生成表名的单复数 public class MYDbContext : DbContext { public DbSet<User> Users { ...

  2. Entity Framework - Func引起的数据库全表查询

    原文:http://www.cnblogs.com/dudu/archive/2012/04/01/enitity_framework_func.html 使用 Entity Framework 最要 ...

  3. 用Entity Framework 来创建MySql数据库和表结构

    1. 创建VS工程 2.添加新项, 选中ADO.Net Entity Data Model模板 3.填入Host及数据库名字, 如果没有此数据库, 会提示创建 4.添加edmx后, 右击选择属性,配置 ...

  4. Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

    DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...

  5. 《Entity Framework 6 Recipes》中文翻译系列 (39) ------ 第七章 使用对象服务之配置模型和使用单复数服务

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-3  配置模型 问题 你想了解配置模型中的各种选项. 解决方案 当你添加一个AD ...

  6. Code First :使用Entity. Framework编程(5) ----转发 收藏

    第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...

  7. 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   ...

  8. 【译著】Code First :使用Entity. Framework编程(5)

    第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...

  9. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

随机推荐

  1. disable_irq与disable_irq_nosync使用场景

    disable_irq与disable_irq_nosync使用场景     Linux设备驱动,关于中断屏蔽有两个接口:disable_irq和disable_irq_nosync,该两接口使用场景 ...

  2. javascirpt对象运用与JS变量

    abcdefghijklmnopqrstuvwyz String 对象方法 charAt() 方法可返回指定位置的字符.stringObject.charAt(index)(index从0开始)[ht ...

  3. iOS去除导航栏和tabbar的1px横线

    1.在自己定义的导航栏中或者设计稿中经常需要去除导航栏的1px横线,主要是颜色太不协调了 去除之前的图片 要去除这1px的横线,首先应该知道它是什么,在Xcode的界面调试中可以看到,它其实是UIIm ...

  4. 【前端】CoffeeScript

    es6中的双箭头函数可以省略小括号,coffee中不可以 es6: (x) => x * x // 可以 x => x * x // 可以 coffee: (x) => x * x ...

  5. I2S/PCM/IOM-2、I2C/SPI/UART/GPIO/slimbus

    概述 I2S,PCM,IOM-2都是数字音频接口,传数据的. I2C,SPI,UART,GPIO是控制接口,传控制信令的. I2S I2S(Inter-IC Sound Bus)是飞利浦公司为数字音频 ...

  6. Oracle客户端配置

    1.  打开开发生产数据库系统,点击下载Oracle_12C_Client32,并且解压缩. 2.  找到文件下的setup.exe文件,并且执行. 3.  等待数秒,在如下界面中选择第二项,管理员, ...

  7. mac os中配置多个jdk(转载+)

    1.首先安装所有的JDk: * Mac自带了的JDK6,安装在目录:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/下. * JDK7,JDK8则 ...

  8. 首师大附中科创教育平台 我的刷题记录 3120 LJX的校园:入学典礼

    今天给大家献上"C"级题:LJX的校园:入学典礼!! 试题编号:3120       LJX的校园:入学典礼 难度级别:C: 运行时间限制:45ms: 运行空间限制:256000K ...

  9. windows系统中WIN键的快捷组合

    1.WIN+E    打开我的电脑(计算机) 2.WIN+D      返回桌面 当你在用聊天工具与朋友聊天时,旁边突然出现某位小伙伴,快速按下WIN+D返回到桌面就可以很好的保护你的聊天内容啦! 3 ...

  10. Linux帮助命令

    帮助命令:man man 命令 获取命令的帮助 如下: [root@localhost ~]# man ls LS(1) User Commands LS(1) NAME ls - list dire ...