zoukankan      html  css  js  c++  java
  • 基于EFCore的CodeFirst实战(一)数据库注解和FluentAPI

    数据库操作

      (1) 数据库迁移  add-migration [任一名称,须唯一] 

      (2) 更新数据库  update-database 

      (3) 删除数据库迁移  remove-migration 


      创建模型,分为数据注释和Fluent API,两者效果一样,看个人习惯二选一

      (1) 主键:按约定,属性名为Id或<type name>Id将配置为实体的键,或者加[Key]指定

    [Key]public string LicensePlate { get; set; }
     modelBuilder.Entity<Car>().HasKey(c => c.LicensePlate);

      (2) 自增长

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]public DateTime Inserted { get; set; }
    modelBuilder.Entity<Blog>().Property(b=>b.Inserted) .ValueGeneratedOnAdd();

      (3) 必填或选填属性

      以下为允许null: string, int?, byte[], decimal?  等

      以下为不允许null: int, decimal, bool  等

    [Required]public string Url { get; set; }
    modelBuilder.Entity<Blog>().Property(b => b.Url).IsRequired();

      (4) 最大长度,仅适用于数组数据类型,如 string 和 byte[] 

    [MaxLength(500)]public string Url { get; set; }
    modelBuilder.Entity<Blog>().Property(b=>b.Url).HasMaxLength(500);

      (5) 自定义数据表格名称

    [Table(“dt_blog”)]public class Blog {
    
    }
    modelBuilder.Entity<Blog>().ToTable("dt_blog");

      (6) 自定义列名

    [Column(“price”)]public DateTime RowVersion { get; set; }

      (7) decimal精度

    [Column(“price”,TypeName ="decimal(12,2)")]public decimal Price { get; set; }
    modelBuilder.Entity<Person>().Property(p=>p.Price).HasColumnName("price").HasColumnType("decimal(12,2)");;

      (8) 批量修改精度,在OnModelCreating中添加以下代码

    复制代码

    foreach (var property in modelBuilder.Model.GetEntityTypes().SelectMany(t => t.GetProperties().Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?))))
            {
    
                    property.Relational().ColumnType = "decimal(18,4)";
    
            }

    复制代码

      (9) 并发令牌

    (MySQL)
    [ConcurrencyCheck]public DateTime RowVersion { get; set; }
    (SQLServer)
    
    [Timestamp]public byte[] RowVersion { get; set; }
    modelBuilder.Entity<Person>().Property(p=>p.RowVersion).IsConcurrencyToken();

      (10) 隐藏属性,指你.NET 实体类中未定义但 EF 核心模型中该实体类型定义

    modelBuilder.Entity<Blog>().Property<DateTime>("LastUpdated");

      (11) 关系,按照约定,发现的类型上的导航属性时,将创建关系。 如果它指向的类型不能将映射为标量类型由当前的数据库提供程序,该属性被视为一个导航属性。

      由于篇幅较长,请参考连接:https://docs.microsoft.com/zh-cn/ef/core/modeling/relationships

    复制代码

    public class Blog {public int BlogId { get; set; }public string Url { get; set; }public List<Post> Posts { get; set; } 
    
    }public class Post {public int PostId { get; set; }public string Title { get; set; }public string Content { get; set; }public int BlogId { get; set; }public Blog Blog { get; set; } 
    
    }
    
     
    
    [ForeignKey("BlogForeignKey")] 
    public Blog Blog { get; set; }
    
     
    
    [InverseProperty("Author")] 
    public List<Post> AuthoredPosts { get; set; }
    
     
    
    [InverseProperty("Contributor")] 
    public List<Post> ContributedToPosts { get; set; }

    复制代码

    modelBuilder.Entity<Post>().HasOne(p=>p.Blog).WithMany(b=>b.Posts);

      (12) 索引

    modelBuilder.Entity<Blog>().HasIndex(b => b.Url).IsUnique();
    
     
    modelBuilder.Entity<Person>().HasIndex(p=>new{p.FirstName,p.LastName });

      (13) 排除属性

    复制代码

    public class Blog {public int BlogId { get; set; }public string Url { get; set; }public BlogMetadata Metadata { get; set; }
    
    }
    
     
    
    [NotMapped]public class BlogMetadata {public DateTime LoadedFromDatabase { get; set; }
    
    }

    复制代码

    modelBuilder.Ignore<BlogMetadata>();

      (14) 排除类型

    复制代码

    public class Blog {public int BlogId { get; set; }public string Url { get; set; }
    
     
    
    [NotMapped]public DateTime LoadedFromDatabase { get; set; }
    
    }

    复制代码

     modelBuilder.Entity<Blog>() .Ignore(b => b.LoadedFromDatabase);
  • 相关阅读:
    一个你爱,一个爱你,你选择哪一个
    Flask 中的 SQLAlchemy 使用教程
    Flas-SQLAchemy数据库操作使用学习笔记
    Flask-SQLAlchemy获取一个字段里去掉重复的数据
    Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
    pycharm快捷键、常用设置、包管理
    [原创]python MySQLdb在windows环境下的安装、出错问题以及解决办法
    Highcharts下载与使用_数据报表图
    Markdown入门指南-指间阁
    Sublime Text3 配置markdown插件
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13124424.html
Copyright © 2011-2022 走看看