zoukankan      html  css  js  c++  java
  • 笔记一、数据库初始化 约定

    1.数据库初始化策略

    EF6

    如果数据库不存在就创建
    Database.SetInitializer(new CreateDatabaseIfNotExists());

    总是创建数据库,无论是否存在
    Database.SetInitializer(new DropCreateDatabaseAlways());

    如果EF检测到数据库木星发生了变化,将更新模型
    Database.SetInitializer(new DropCreateDatabaseIfModelChanges());

    public class EfDbContext:System.Data.Entity.DbContext
        {
            public EfDbContext():base("name=ConnectionString")
            {
                //如果数据库不存在就创建
                //Database.SetInitializer(new CreateDatabaseIfNotExists<EfDbContext>());
    
                //总是创建数据库,无论是否存在
                //Database.SetInitializer(new DropCreateDatabaseAlways<EfDbContext>());
    
                //如果EF检测到数据库木星发生了变化,将更新模型
                //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EfDbContext>());
            }
    }
    

    2.自定义约定

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
                //1.全局约定
                //属性名为Id的全部配置为主键
                modelBuilder.Properties().Where(x=>x.Name=="Id").Configure(p=>p.IsKey());
                //属性为decimal类型的全部配置位数为十位,全部保留2个小数点
                modelBuilder.Properties<decimal>().Configure(configure=>configure.HasPrecision(10,2));
    
                //2.执行约定
                //属性为string类型的全部设置长度500
                modelBuilder.Properties<string>().Configure(c=>c.HasMaxLength(500));
                //属性为string类型的且属性名为Name的全部设置长度250
                modelBuilder.Properties<string>().Where(x=>x.Name=="Name").Configure(c=>c.HasMaxLength(250));
    
                //3.内置约定
                modelBuilder.Conventions.AddBefore<IdKeyDiscoveryConvention>(new DateTime2Convention());
    
                //4.自定义类约束
                modelBuilder.Conventions.Add(new DateTime2Convention());
    
                //6.自定义类型约束
                modelBuilder.Types().Configure(c=>c.ToTable(GetTableName(c.ClrType)));
            }
    
            //自定义约束类型
            public class DateTime2Convention : Convention
            {
                public DateTime2Convention()
                {
                    //将所有类型为datetime的属性映射到类型为datetime2的数据库中
                    this.Properties<DateTime>().Configure(c=>c.HasColumnType("datetime2"));
                }
            }
    
            private string GetTableName(Type type)
            {
                var result = Regex.Replace(type.Name, ".[A-Z]", m => m.Value[0] + "_" + m.Value[1]);
                return result.ToLower();
    	}
    }
    

    Replace(char oldChar, char newChar)
    将此实例中的指定 Unicode 字符的所有匹配项替换为其他指定的 Unicode 字符

    仅为个人笔记,不喜欢勿吐槽

  • 相关阅读:
    利用 PhpStorm、Idea 等 IDE 如何 运行/调试 Go 程序 ?
    [Go] 函数/方法 的 变参
    PHP 如何显示大数字,防止显示为 科学计数法 形式
    PHP协程 详解
    [Go] 路径、目录名、包名、文件名
    [Go] 复合类型(数组、切片、字典、结构体)变量的 初始化 及 注意事项
    Firefox 及其 插件“个性化设置”备份
    Go
    [Go] template 常用方法详解 及 注意事项
    Go
  • 原文地址:https://www.cnblogs.com/zuiren/p/10849920.html
Copyright © 2011-2022 走看看