基于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);
相关推荐
CoderToy 2020-11-16
技术之博大精深 2020-10-16
emmm00 2020-11-17
bianruifeng 2020-11-16
云中舞步 2020-11-12
世樹 2020-11-11
暗夜之城 2020-11-11
张荣珍 2020-11-12
amienshxq 2020-11-14
ASoc 2020-11-14
yungpheng 2020-10-19
loveyouluobin 2020-09-29
尘封飞扬 2020-09-29
Coder技术文摘 2020-09-29
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09