zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    HttpURLconnection的介绍
    HttpClient4.3教程 第三章 Http状态管理
    HttpClient4.3教程 第二章 连接管理
    HttpClient 4.3教程 第一章 基本概念
    HttpClient 4.3教程-前言
    数据结构
    数据结构
    HashMap底层源码剖析
    防止XSS 攻击集成springboot
    C——Network Saboteur (POJ2531)
  • 原文地址:https://www.cnblogs.com/smallerpig/p/3654311.html
Copyright © 2011-2022 走看看