zoukankan      html  css  js  c++  java
  • MVC 建立模型/实体类,对应到数据库

    建的模型需要有PK主关键字,默认为’Id’或者‘实体名Id',如果PK不是这两个,要先添加[Key]到PK上方。

        public partial class BookInfo : BaseEntity
        {
            [Key]
            public string BookId { get; set; }
            public string BookName { get; set; }
        }

    另外说一点,这个BookInfo类继承了BaseEntity这个实体属性基类,那么这个BookInfo也需要有BaseEntity的字段,要不会报错。

        [Serializable]
        public class BaseEntity
        {
            public int EntityId { get; set; }
    
            private DateTime _CreateTime = DateTime.Now;
    
            public DateTime CreateTime
            {
                set { _CreateTime = value; }
                get { return _CreateTime; }
            }
        }

    数据库表为Book,那么需要定义Mapping,Mapping可以和Domain放一块。

    并且移除EF的表名公约(移除复数表名的契约 ),在定义数据库连接的时候。

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // 移除EF的表名公约 ,移除复数表名的契约 
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
                // 还可以移除对MetaData表的查询验证   防止黑幕交易 要不然每次都要访问 EdmMetadata这个表
                //modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 
                modelBuilder.Conventions.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
    
                modelBuilder.Configurations.Add(new BookMap());
            }
  • 相关阅读:
    Maven 打war包
    linuxan安装redis出现Newer version of jemalloc required错误
    Linux常用命令
    Linux重启和关机命令
    CentOS 端口和防火墙操作
    修改root密码
    Web登录验证之 Shiro
    No WebApplicationContext found: no ContextLoaderListener registered
    java写文件实现换行
    gcc命令详解
  • 原文地址:https://www.cnblogs.com/llk8/p/3505005.html
Copyright © 2011-2022 走看看