基于 ASP.NET Core 2.0 WebAPI 后台框架搭建(4) - EF Core CodeFirst 数据库创建 基于 ASP.NET Core 2.0 WebAPI 后台框架搭建(2) - EF Core (MySQL) CodeFirst 数据库迁移与依赖注入

  概述

  在 基于 ASP.NET Core 2.0 WebAPI 后台框架搭建(2) - EF Core (MySQL) CodeFirst 数据库迁移与依赖注入 一文中,我们介绍如何快速以CodeFirst快速搭建数据库,这一章,我们来完善一下创建数据库中可以添加的验证与约束。


  数据库操作

  (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);

猜你喜欢

转载自www.cnblogs.com/loda7023link/p/9210862.html