EntityFramework Core笔记:表结构及数据基本操作(2)
1. 表结构操作
1.1 表名
Data Annotations:
using System.ComponentModel.DataAnnotations.Schema;
[Table("Role")] public class Role { // ... }
FluentAPI:
using Microsoft.EntityFrameworkCore;
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Role>().ToTable("Role"); }
1.2 字段
Data Annotations:
using System; using System.Collections.Generic; using System.Text; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Libing.App.Models.Entities { [Table("Role")] public class Role { [Column("RoleID")] public int RoleID { get; set; } [Required] [Column("RoleName",TypeName = "varchar(200)")] public string RoleName { get; set; } } }
FluentAPI:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Role>().ToTable("Role"); modelBuilder.Entity<Role>() .Property(t => t.RoleID) .HasColumnName("RoleID"); modelBuilder.Entity<Role>() .Property(t => t.RoleName) .HasColumnName("RoleName") .HasColumnType("varchar(200)") //.HasMaxLength(200) .IsRequired(); }
1.3 主键
Data Annotations:
[Table("Role")] public class Role { [Key] public int RoleID { get; set; } }
FluentAPI:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Role>() .HasKey(t => t.RoleID); // 复合主键 //modelBuilder.Entity<Role>() // .HasKey(t => new { t.RoleID, t.RoleName }); }
1.4 计算列
FluentAPI:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<User>() .Property(t => t.DisplayName) .HasComputedColumnSql("[LastName] + ',' + [FirstName]"); }
CREATE TABLE [Users] ( [UserID] int NOT NULL IDENTITY, [DisplayName] AS [LastName] + ',' + [FirstName], [FirstName] nvarchar(max) NULL, [LastName] nvarchar(max) NULL, CONSTRAINT [PK_Users] PRIMARY KEY ([UserID]) );
1.5 生成值
主键属性如果是整数或Guid类型,该属性将会被EntityFramework Core设置为自动生成。
Data Annotations:
(1)没有生成值
[Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int RoleID { get; set; }
(2)添加操作生成值
[Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int RoleID { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public DateTime ModifiedDate { get; set; }
(3)添加或修改操作生成值
[DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime ModifiedDate { get; set; }
FluentAPI:
modelBuilder.Entity<Role>() .Property(t => t.RoleID) .ValueGeneratedOnAdd();
modelBuilder.Entity<Role>() .Property(t => t.ModifiedDate) .ValueGeneratedNever();
modelBuilder.Entity<Role>() .Property(t => t.ModifiedDate) .ValueGeneratedOnAdd();
modelBuilder.Entity<Role>() .Property(t => t.ModifiedDate) .ValueGeneratedOnAddOrUpdate();
注:按照约定,非复合主键的类型 short、 int、 long、 或 Guid 将安装程序能够生成上添加的值。
所有其他属性将与不值生成的安装程序。
1.6 默认值
modelBuilder.Entity<Role>() .Property(t => t.RoleName) .HasDefaultValue(String.Empty);
modelBuilder.Entity<Role>() .Property(t => t.ModifiedDate) .HasDefaultValueSql("GETDATE()");
CREATE TABLE [Role] ( [RoleID] int NOT NULL IDENTITY, [ModifiedDate] datetime2 NOT NULL DEFAULT (GETDATE()), [RoleName] varchar(200) NOT NULL DEFAULT N'', CONSTRAINT [PK_Role] PRIMARY KEY ([RoleID]) );
1.7 序列
modelBuilder.HasSequence<int>("RoleSequence", "dbo") .StartsAt(1) .IncrementsBy(1);
modelBuilder.Entity<Role>() .Property(t => t.RoleID) .HasDefaultValueSql("NEXT VALUE FOR dbo.RoleSequence");
CREATE SEQUENCE [dbo].[RoleSequence] AS int START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
CREATE TABLE [dbo].[Role] ( [RoleID] int NOT NULL DEFAULT (NEXT VALUE FOR dbo.RoleSequence), [ModifiedDate] datetime2 NOT NULL, [RoleName] varchar(200) NOT NULL, CONSTRAINT [PK_Role] PRIMARY KEY ([RoleID]) );
1.8 索引
modelBuilder.Entity<Role>() .HasIndex(t => t.RoleName) .HasName("IX_RoleName");
CREATE INDEX [IX_RoleName] ON [dbo].[Role] ([RoleName]);
modelBuilder.Entity<Role>() .HasIndex(t => t.RoleName) .IsUnique() .HasName("IX_RoleName");
CREATE UNIQUE INDEX [IX_RoleName] ON [dbo].[Role] ([RoleName]);
1.9 默认架构
modelBuilder.HasDefaultSchema("dbo");
1.10 完整实体类配置
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Role>(entity => { // Primary Key entity.HasKey(t => t.RoleID); // Properties entity.Property(t => t.RoleName).HasMaxLength(100); // Table & Column Mappings entity.ToTable("Role", "dbo"); entity.Property(t => t.RoleID).HasColumnName("RoleID"); entity.Property(t => t.RoleName).HasColumnName("RoleName"); }); }
RoleConfiguration.cs
using System; using System.Collections.Generic; using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Libing.App.Models.Entities; namespace Libing.App.Models.Configurations { public class RoleConfiguration : IEntityTypeConfiguration<Role> { public void Configure(EntityTypeBuilder<Role> builder) { // Primary Key builder.HasKey(t => t.RoleID); // Properties builder.Property(t => t.RoleName) .IsRequired() .HasMaxLength(100); // Table & Column Mappings builder.ToTable("Role", "dbo"); builder.Property(t => t.RoleID).HasColumnName("RoleID"); builder.Property(t => t.RoleName).HasColumnName("RoleName"); } } }
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new RoleConfiguration()); }