博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework – (复数)Plural and (单数)Singular 表名Table names
阅读量:6543 次
发布时间:2019-06-24

本文共 1560 字,大约阅读时间需要 5 分钟。

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
Products { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove
(); } }

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

 

之所以这样,是因为开发者就表名是否使用复数没有达成一致。这个教程使用了单数形式,但重点是您可以自己选择使用哪种形式来命名。

参考:

转载于:https://www.cnblogs.com/smallerpig/p/3654311.html

你可能感兴趣的文章
ttcn-3
查看>>
00.java虚拟机的基本结构概念
查看>>
深入浅出 ES6:ES6 与 Babel - Broccoli 的联用
查看>>
ThreadLocal使用出现的问题
查看>>
关于十六进制和八进制负数的问题
查看>>
连接池并发的实现原理
查看>>
创建Pch预编译文件
查看>>
阿里云Centos配置iptables防火墙
查看>>
UML类图几种关系的总结
查看>>
PHP面试题汇总
查看>>
LeetCode (11): Container With Most Water
查看>>
【技巧】easyUI的datagrid,如何在翻页以后仍能记录被选中的行
查看>>
经过强制类型转换以后,变量a, b的值分别为( )short a = 128; byte b = (byte) a;
查看>>
ubuntu下msmtp+mutt的安装和配置
查看>>
spring中注解说明
查看>>
QLabel显示图片,图片可以自适应label的大小
查看>>
阅读下面程序,请回答如下问题:
查看>>
BZOJ3994:[SDOI2015]约数个数和——题解
查看>>
3、EJB3.0开发第一个无会话Bean和客户端(jboss4.2.3)
查看>>
git fetch & pull详解
查看>>