Entity Framework Core

Entity Framework (EF) Core 是轻量化、可扩展、开源和跨平台版的常用 Entity Framework 数据访问技术。

EF Core 可用作对象关系映射程序 (O/RM),这可以实现以下两点:

使 .NET 开发人员能够使用 .NET 对象处理数据库。

无需再像通常那样编写大部分数据访问代码。

EF 支持以下模型开发方法:

  • Database First:先有数据库,后有实体模型。从现有数据库生成模型。对模型手动编码,使其符合数据库。
  • Code First:创建模型后,使用 EF 迁移从模型创建数据库。 模型发生变化时,迁移可让数据库不断演进。先有实体模型,后生成数据库

创建模型

通用模型

数据表,都会有主键字段。为了满足此需求,所以我们建立一个通用的泛型模型BaseEntity

  1. /// <summary>
  2. /// 通用基本模型
  3. /// </summary>
  4. /// <typeparam name="K">主键类型</typeparam>
  5. public class BaseEntity<K>
  6. {
  7. /// <summary>
  8. /// Id,主键
  9. /// </summary>
  10. public K Id { get; set; }
  11. }
  12. /// <summary>
  13. /// 通用模型
  14. /// </summary>
  15. public class BaseEntity : BaseEntity<string>
  16. {
  17. }

为了让系统知道我们的Id字段是主键,我们增加了对通用模型的配置

EF可以有两种方法来配置实体,一种是使用数据批注(Attribute)

还有一种是Fluent API,就是通过代码来对模型配置

本示例使用Fluent API来配置

因为有些特殊情况是不能使用数据批注(Attribute)来满足要求

比如多主键,多外键,关联等情况。

  1. /// <summary>
  2. /// 默认实体配置
  3. /// OnModelCreating
  4. /// </summary>
  5. /// <typeparam name="T"></typeparam>
  6. /// <typeparam name="K"></typeparam>
  7. public class BaseEntityTypeConfig<T, K> : IEntityTypeConfiguration<T>
  8. where T : BaseEntity<K>
  9. {
  10. public virtual void Configure(EntityTypeBuilder<T> builder)
  11. {
  12. #region 主外键关系
  13. builder.HasKey(k => k.Id);//设置主键
  14. #endregion
  15. #region 字段属性:最大长度,是否必需,默认值
  16. #endregion
  17. #region 备注
  18. builder.Property(p => p.Id).HasComment("主键");//设置备注
  19. #endregion
  20. }
  21. }
  22. public class BaseEntityTypeConfig<T> : BaseEntityTypeConfig<T, string>, IEntityTypeConfiguration<T>
  23. where T : BaseEntity
  24. {
  25. public override void Configure(EntityTypeBuilder<T> builder)
  26. {
  27. base.Configure(builder);
  28. #region 主外键关系
  29. #endregion
  30. #region 字段属性:最大长度,是否必需,默认值
  31. builder.Property(p => p.Id).HasMaxLength(50);//设置主键最大长度50
  32. #endregion
  33. #region 备注
  34. #endregion
  35. }
  36. }

对于业务实体,一般我们又会有些其它通过字段,比如创建人,创建时间,修改人,修改时间,是否删除等公共字段

所以我们创建了BusEntity通用业务模型

  1. /// <summary>
  2. /// 业务实体基类
  3. /// </summary>
  4. /// <typeparam name="K">主键类型</typeparam>
  5. public class BusEntity<K> : BaseEntity<K>
  6. {
  7. /// <summary>
  8. /// 是否删除
  9. /// </summary>
  10. public bool Deleted { get; set; }
  11. /// <summary>
  12. /// 创建人
  13. /// </summary>
  14. public K CreateUserId { get; set; }
  15. /// <summary>
  16. /// 创建时间
  17. /// </summary>
  18. public DateTime CreateTime { get; set; }
  19. /// <summary>
  20. /// 修改人
  21. /// </summary>
  22. public K ModifyUserId { get; set; }
  23. /// <summary>
  24. /// 修改时间
  25. /// </summary>
  26. public DateTime ModifyTime { get; set; }
  27. }
  28. /// <summary>
  29. /// 业务实体基类
  30. /// </summary>
  31. public class BusEntity : BusEntity<string>
  32. { }

对于基本业务实体基类用以下配置

  1. /// <summary>
  2. /// 默认实体配置
  3. /// OnModelCreating
  4. /// </summary>
  5. /// <typeparam name="T"></typeparam>
  6. /// <typeparam name="K"></typeparam>
  7. public class BusEntityTypeConfig<T, K> : BaseEntityTypeConfig<T, K>, IEntityTypeConfiguration<T>
  8. where T : BusEntity<K>
  9. {
  10. public override void Configure(EntityTypeBuilder<T> builder)
  11. {
  12. base.Configure(builder);
  13. builder.HasQueryFilter(q => q.Deleted == false);//查询自动过滤已经删除的记录
  14. #region 主外键关系
  15. #endregion
  16. #region 字段属性:最大长度,是否必需,默认值
  17. builder.Property(p => p.Deleted).HasDefaultValue(false);//把是否删除设置为默认False
  18. builder.Property(p => p.CreateUserId).HasMaxLength(50);//把创建人设置为默认值
  19. builder.Property(p => p.ModifyUserId).HasMaxLength(50);//把修改人设置为默认值
  20. builder.Property(p => p.CreateTime).HasDefaultValueSql("getdate()").ValueGeneratedOnAdd();//把创建时间设置默认值并在增加的时候更新值
  21. builder.Property(p => p.ModifyTime).HasDefaultValueSql("getdate()").ValueGeneratedOnAddOrUpdate();//把修改时间设置默认值并在增加和修改的时候更新值
  22. #endregion
  23. #region 备注
  24. builder.Property(p => p.Deleted).HasComment("是否删除");
  25. builder.Property(p => p.CreateUserId).HasComment("创建人");
  26. builder.Property(p => p.CreateTime).HasComment("创建时间");
  27. builder.Property(p => p.ModifyUserId).HasComment("修改人");
  28. builder.Property(p => p.ModifyTime).HasComment("修改时间");
  29. #endregion
  30. }
  31. }
  32. public class BusEntityTypeConfig<T> : BusEntityTypeConfig<T, string>, IEntityTypeConfiguration<T>
  33. where T : BusEntity
  34. {
  35. public override void Configure(EntityTypeBuilder<T> builder)
  36. {
  37. base.Configure(builder);
  38. #region 主外键关系
  39. #endregion
  40. #region 字段属性:最大长度,是否必需,默认值
  41. builder.Property(p => p.Id).HasMaxLength(50);
  42. #endregion
  43. #region 备注
  44. #endregion
  45. }
  46. }

业务模型

接下来我们有了通用模型基类。那么我们就可以来创建具体的业务模型

比如说我们的组织架构模型

我们使用两个局部类来定义,

第一个局部类定义基本属性

第二个局部类定义关联关系

然后对模型进行配置

  1. /// <summary>
  2. /// 组织架构
  3. /// </summary>
  4. public partial class Sys_Org : BusEntity
  5. {
  6. /// <summary>
  7. /// 上级组织
  8. /// </summary>
  9. public string ParentId { get; set; }
  10. /// <summary>
  11. /// 名称
  12. /// </summary>
  13. public string Name { get; set; }
  14. }
  15. public partial class Sys_Org : BusEntity
  16. {
  17. /// <summary>
  18. /// 上级组织
  19. /// </summary>
  20. public Sys_Org Parent { get; set; }
  21. /// <summary>
  22. /// 下级组织
  23. /// </summary>
  24. public List<Sys_Org> Childs { get; set; }
  25. }
  26. /// <summary>
  27. /// 实体配置
  28. /// OnModelCreating
  29. /// </summary>
  30. public class Sys_OrgTypeConfig : BusEntityTypeConfig<Sys_Org>, IEntityTypeConfiguration<Sys_Org>
  31. {
  32. public override void Configure(EntityTypeBuilder<Sys_Org> builder)
  33. {
  34. base.Configure(builder);
  35. #region 主外键关系
  36. builder.HasOne(p => p.Parent).WithMany(p => p.Childs).HasForeignKey(p => p.ParentId);
  37. #endregion
  38. #region 字段属性:最大长度,是否必需,默认值
  39. builder.Property(p => p.Name).HasMaxLength(50).IsRequired();
  40. #endregion
  41. #region 备注
  42. builder.Property(p => p.ParentId).HasComment("上级组织");
  43. builder.Property(p => p.Name).HasComment("名称");
  44. #endregion
  45. }
  46. }
  1. /// <summary>
  2. /// 系统用户
  3. /// </summary>
  4. public partial class Sys_User : BusEntity
  5. {
  6. /// <summary>
  7. /// 工号、编码
  8. /// </summary>
  9. public string Code { get; set; }
  10. /// <summary>
  11. /// 名称
  12. /// </summary>
  13. public string Name { get; set; }
  14. /// <summary>
  15. /// 用户名
  16. /// </summary>
  17. public string UserName { get; set; }
  18. /// <summary>
  19. /// 密码
  20. /// </summary>
  21. public string Password { get; set; }
  22. /// <summary>
  23. /// 状态
  24. /// </summary>
  25. public string Status { get; set; }
  26. /// <summary>
  27. /// 所属组织
  28. /// </summary>
  29. public string OrgId { get; set; }
  30. /// <summary>
  31. /// 性别
  32. /// </summary>
  33. public string Sex { get; set; }
  34. }
  35. public partial class Sys_User : BusEntity
  36. {
  37. /// <summary>
  38. /// 所属组织
  39. /// </summary>
  40. public Sys_Org Org { get; set; }
  41. }
  42. /// <summary>
  43. /// 实体配置
  44. /// OnModelCreating
  45. /// </summary>
  46. public class Sys_UserTypeConfig : BusEntityTypeConfig<Sys_User>, IEntityTypeConfiguration<Sys_User>
  47. {
  48. public override void Configure(EntityTypeBuilder<Sys_User> builder)
  49. {
  50. base.Configure(builder);
  51. #region 主外键关系
  52. builder.HasOne(p => p.Org).WithMany().HasForeignKey(p => p.OrgId);
  53. #endregion
  54. #region 字段属性:最大长度,是否必需,默认值
  55. builder.Property(p => p.Code).HasMaxLength(50);
  56. builder.Property(p => p.Name).HasMaxLength(50).IsRequired();
  57. builder.Property(p => p.UserName).HasMaxLength(50).IsRequired();
  58. builder.Property(p => p.Password).HasMaxLength(100).IsRequired();
  59. builder.Property(p => p.Status).HasMaxLength(50).IsRequired();
  60. builder.Property(p => p.OrgId).HasMaxLength(50);
  61. builder.Property(p => p.Sex).HasMaxLength(50);
  62. #endregion
  63. #region 备注
  64. builder.Property(p => p.Code).HasComment("编码");
  65. builder.Property(p => p.Name).HasComment("名称");
  66. builder.Property(p => p.UserName).HasComment("用户名");
  67. builder.Property(p => p.Password).HasComment("密码");
  68. builder.Property(p => p.Status).HasComment("状态");
  69. builder.Property(p => p.OrgId).HasComment("所属组织");
  70. builder.Property(p => p.Sex).HasComment("性别");
  71. #endregion
  72. }
  73. }

创建数据库上下文Context

有了数据模型,接下来我们创建数据库上下文Context

OnConfiguring用来配置数据连接字符串

OnModelCreating是创建模型是对模型的配置

因为上面我们对每个模型都做了配置(实现了IEntityTypeConfiguration)

所以在这里我们只要把具体的配置配置应用到Context就行了

我们使用modelBuilder.ApplyConfigurationsFromAssembly

就可以把所有实现了IEntityTypeConfiguration的模型配置全部应用到数据库上下文

  1. public class GDbContext : DbContext
  2. {
  3. /// <summary>
  4. /// Context配置
  5. /// </summary>
  6. /// <param name="optionsBuilder"></param>
  7. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  8. {
  9. base.OnConfiguring(optionsBuilder);
  10. optionsBuilder.UseSqlServer("Data Source=x.x.x.x;Initial Catalog=数据库名称;User Id=用户名;Password=密码;APP=系统名称;Pooling=true;");
  11. }
  12. /// <summary>
  13. /// 模型创建
  14. /// </summary>
  15. /// <param name="modelBuilder"></param>
  16. protected override void OnModelCreating(ModelBuilder modelBuilder)
  17. {
  18. base.OnModelCreating(modelBuilder);
  19. modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
  20. }
  21. /// <summary>
  22. /// 组织架构
  23. /// </summary>
  24. public DbSet<Sys_Org> Sys_Org { get; set; }
  25. /// <summary>
  26. /// 用户
  27. /// </summary>
  28. public DbSet<Sys_User> Sys_User { get; set; }
  29. }

所有代码结构如下

迁移数据库

专业名称叫迁移,通用解释就是把实体模型生成为数据表

我们在OnConfiguring中已经配置了使用SqlServer数据库(当然也支持其它所有类型数据库:如MySql,Oracle,Sqlite等其它数据库)

使用迁移,必需安装Microsoft.EntityFrameworkCore.Tools工具集

接下来我们在“程序包管理控制台”输入Add-Migration FirstInit (FirstInit:自定义名称)

系统会自动帮我们生成迁移代码



  1. public partial class FirstInit : Migration
  2. {
  3. protected override void Up(MigrationBuilder migrationBuilder)
  4. {
  5. migrationBuilder.CreateTable(
  6. name: "Sys_Org",
  7. columns: table => new
  8. {
  9. Id = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "主键"),
  10. ParentId = table.Column<string>(type: "nvarchar(50)", nullable: true, comment: "上级组织"),
  11. Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "名称"),
  12. Deleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "是否删除"),
  13. CreateUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "创建人"),
  14. CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "创建时间"),
  15. ModifyUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "修改人"),
  16. ModifyTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "修改时间")
  17. },
  18. constraints: table =>
  19. {
  20. table.PrimaryKey("PK_Sys_Org", x => x.Id);
  21. table.ForeignKey(
  22. name: "FK_Sys_Org_Sys_Org_ParentId",
  23. column: x => x.ParentId,
  24. principalTable: "Sys_Org",
  25. principalColumn: "Id",
  26. onDelete: ReferentialAction.Restrict);
  27. });
  28. migrationBuilder.CreateTable(
  29. name: "Sys_User",
  30. columns: table => new
  31. {
  32. Id = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "主键"),
  33. Code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "编码"),
  34. Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "名称"),
  35. UserName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "用户名"),
  36. Password = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, comment: "密码"),
  37. Status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "状态"),
  38. OrgId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "所属组织"),
  39. Sex = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "性别"),
  40. Deleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "是否删除"),
  41. CreateUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "创建人"),
  42. CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "创建时间"),
  43. ModifyUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "修改人"),
  44. ModifyTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "修改时间")
  45. },
  46. constraints: table =>
  47. {
  48. table.PrimaryKey("PK_Sys_User", x => x.Id);
  49. table.ForeignKey(
  50. name: "FK_Sys_User_Sys_Org_OrgId",
  51. column: x => x.OrgId,
  52. principalTable: "Sys_Org",
  53. principalColumn: "Id",
  54. onDelete: ReferentialAction.Restrict);
  55. });
  56. migrationBuilder.CreateIndex(
  57. name: "IX_Sys_Org_ParentId",
  58. table: "Sys_Org",
  59. column: "ParentId");
  60. migrationBuilder.CreateIndex(
  61. name: "IX_Sys_User_OrgId",
  62. table: "Sys_User",
  63. column: "OrgId");
  64. }
  65. protected override void Down(MigrationBuilder migrationBuilder)
  66. {
  67. migrationBuilder.DropTable(
  68. name: "Sys_User");
  69. migrationBuilder.DropTable(
  70. name: "Sys_Org");
  71. }
  72. }

更新到数据库

我们通过Update-Database命令更新到数据库



生成SQL脚本更新到生产数据库

有时候我们系统已经在运行了。开始也不可能直接连接生产数据库

那么我们修改了模型,可以通过生成SQL脚本,来更新数据库

  1. #起始迁移点(没有写0)
  2. #结束迁移点
  3. Script-Migration -From 0 -To 20210225022927_FirstInit

  1. IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
  2. BEGIN
  3. CREATE TABLE [__EFMigrationsHistory] (
  4. [MigrationId] nvarchar(150) NOT NULL,
  5. [ProductVersion] nvarchar(32) NOT NULL,
  6. CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
  7. );
  8. END;
  9. GO
  10. BEGIN TRANSACTION;
  11. GO
  12. CREATE TABLE [Sys_Org] (
  13. [Id] nvarchar(50) NOT NULL,
  14. [ParentId] nvarchar(50) NULL,
  15. [Name] nvarchar(50) NOT NULL,
  16. [Deleted] bit NOT NULL DEFAULT CAST(0 AS bit),
  17. [CreateUserId] nvarchar(50) NULL,
  18. [CreateTime] datetime2 NOT NULL DEFAULT (getdate()),
  19. [ModifyUserId] nvarchar(50) NULL,
  20. [ModifyTime] datetime2 NOT NULL DEFAULT (getdate()),
  21. CONSTRAINT [PK_Sys_Org] PRIMARY KEY ([Id]),
  22. CONSTRAINT [FK_Sys_Org_Sys_Org_ParentId] FOREIGN KEY ([ParentId]) REFERENCES [Sys_Org] ([Id]) ON DELETE NO ACTION
  23. );
  24. DECLARE @defaultSchema AS sysname;
  25. SET @defaultSchema = SCHEMA_NAME();
  26. DECLARE @description AS sql_variant;
  27. SET @description = N'主键';
  28. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'Id';
  29. SET @description = N'上级组织';
  30. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'ParentId';
  31. SET @description = N'名称';
  32. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'Name';
  33. SET @description = N'是否删除';
  34. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'Deleted';
  35. SET @description = N'创建人';
  36. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'CreateUserId';
  37. SET @description = N'创建时间';
  38. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'CreateTime';
  39. SET @description = N'修改人';
  40. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'ModifyUserId';
  41. SET @description = N'修改时间';
  42. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'ModifyTime';
  43. GO
  44. CREATE TABLE [Sys_User] (
  45. [Id] nvarchar(50) NOT NULL,
  46. [Code] nvarchar(50) NULL,
  47. [Name] nvarchar(50) NOT NULL,
  48. [UserName] nvarchar(50) NOT NULL,
  49. [Password] nvarchar(100) NOT NULL,
  50. [Status] nvarchar(50) NOT NULL,
  51. [OrgId] nvarchar(50) NULL,
  52. [Sex] nvarchar(50) NULL,
  53. [Deleted] bit NOT NULL DEFAULT CAST(0 AS bit),
  54. [CreateUserId] nvarchar(50) NULL,
  55. [CreateTime] datetime2 NOT NULL DEFAULT (getdate()),
  56. [ModifyUserId] nvarchar(50) NULL,
  57. [ModifyTime] datetime2 NOT NULL DEFAULT (getdate()),
  58. CONSTRAINT [PK_Sys_User] PRIMARY KEY ([Id]),
  59. CONSTRAINT [FK_Sys_User_Sys_Org_OrgId] FOREIGN KEY ([OrgId]) REFERENCES [Sys_Org] ([Id]) ON DELETE NO ACTION
  60. );
  61. DECLARE @defaultSchema AS sysname;
  62. SET @defaultSchema = SCHEMA_NAME();
  63. DECLARE @description AS sql_variant;
  64. SET @description = N'主键';
  65. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Id';
  66. SET @description = N'编码';
  67. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Code';
  68. SET @description = N'名称';
  69. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Name';
  70. SET @description = N'用户名';
  71. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'UserName';
  72. SET @description = N'密码';
  73. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Password';
  74. SET @description = N'状态';
  75. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Status';
  76. SET @description = N'所属组织';
  77. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'OrgId';
  78. SET @description = N'性别';
  79. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Sex';
  80. SET @description = N'是否删除';
  81. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Deleted';
  82. SET @description = N'创建人';
  83. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'CreateUserId';
  84. SET @description = N'创建时间';
  85. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'CreateTime';
  86. SET @description = N'修改人';
  87. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'ModifyUserId';
  88. SET @description = N'修改时间';
  89. EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'ModifyTime';
  90. GO
  91. CREATE INDEX [IX_Sys_Org_ParentId] ON [Sys_Org] ([ParentId]);
  92. GO
  93. CREATE INDEX [IX_Sys_User_OrgId] ON [Sys_User] ([OrgId]);
  94. GO
  95. INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
  96. VALUES (N'20210225022927_FirstInit', N'5.0.3');
  97. GO
  98. COMMIT;
  99. GO

这样的话。就只要把这个脚本放到生产环境去运行,

那么得到的结果也是和Update-Database结果是一样的

使用EF的Code First模式创建模型的更多相关文章

  1. (转载)EF 使用code first模式创建数据库和 填充种子数据

    第一篇:来自 .net 开发菜鸟 博主的文章:https://www.cnblogs.com/dotnet261010/p/8035213.html 第二篇:来自 JustYong 博主的文章:htt ...

  2. EntityFramework使用Code First模式创建数据库控制生成单数形式的表名

    使用Code-First模式生成数据库时,默认生成的数据库表的名称为类型的复数形式,例如实体类名称是"User",默认生成的数据库表名为“Users”,多数情况下我们并不想生成的数 ...

  3. 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能

    此处采用VS2017+SqlServer数据库 一.创建项目并引用dll: 1.创建一个MVC项目 2.采用Nuget安装EF6.1.3 二.创建Model 在models文件夹中,建立相应的mode ...

  4. EF应用一:Code First模式

    EF的核心程序集位于System.Data.Entity.dll和System.Data.EntityFramework.dll中.支持CodeFirst的位于EntityFramework.dll中 ...

  5. 8天掌握EF的Code First开发系列之2 Code First开发系列之领域建模和管理实体关系

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 理解Code First及其约定和配置 创建数据表结构 管理实体关系 三种继承模式 本章小结 本人的实验环境是V ...

  6. Entity Framework应用:使用Code First模式管理视图

    一.什么是视图 视图在RDBMS(关系型数据库管理系统)中扮演了一个重要的角色,它是将多个表的数据联结成一种看起来像是一张表的结构,但是没有提供持久化.因此,可以将视图看成是一个原生表数据顶层的一个抽 ...

  7. [EF] - 动态创建模型:System.Reflection.Emit + Code First

    动态创建Entity Framework模型并且创建数据库 使用System.Reflection.Emit+Code First model创建以下的一个实体类和DbContext并且创建数据库: ...

  8. 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...

  9. EF 中 Code First 的数据迁移以及创建视图

    写在前面: EF 中 Code First 的数据迁移网上有很多资料,我这份并没什么特别.Code First 创建视图网上也有很多资料,但好像很麻烦,而且亲测好像是无效的方法(可能是我太笨,没搞成功 ...

随机推荐

  1. cassandra权威指南读书笔记--客户端

    DataStax驱动最成熟.默认,驱动程序会使用第一个连接的节点作为支持的版本协议.如果集群存在高低版本的节点(比如升级场景),如果驱动先连接不同不同版本的节点,可能会出现不兼容.驱动支持压缩客户端和 ...

  2. UI的管理

    游戏的UI系统往往会比较复杂,工作量比较庞大,需要多人协作完成,为了开发和维护方便,有必要对UI系统进行管理. 一.制作预制件 将UI的各个不同的功能面板制作为预制件,放入Resources目录下,方 ...

  3. ES6(三) Promise 的基本使用方式

    基本用法 关于Promise的资料,网上有很多了,这里简单粗暴一点,直接上代码. 假设我们要做一个访问后端API的函数,那么我们可以这样模拟一下. const mySend = (url, data) ...

  4. 【洛谷 p3383】模板-线性筛素数(数论)

    题目:给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内).(N<=10000000,M<=100000) 解法:1.欧拉筛O(n),数组近乎100KB:2.( ...

  5. 2019牛客多校 Round8

    Solved:3 Rank:261 E Explorer (线段树) 题意:n个点 m条边 每条边只有身高l,r内的人可以穿过 问有几种身高可以从1走到n 题解:把l,r离散化后(左闭右开) 线段树叶 ...

  6. cf-1230C Anadi and Domino

    题目链接:http://codeforces.com/contest/1230/problem/C 题意: 有21 个多米诺骨牌,给定一个无向图(无自环,无重边),一条边上可以放一个多米诺骨牌.如果两 ...

  7. Codeforces Round #672 (Div. 2 B. Rock and Lever (位运算)

    题意:给你一组数,求有多少对\((i,j)\),使得\(a_{i}\)&\(a_{j}\ge a_{i}\ xor\ a_{j}\). 题解:对于任意两个数的二进制来说,他们的最高位要么相同要 ...

  8. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  9. Linux-输出/输入重定向

    目录 重定向的分类 输出重定向 将标准输出重定向到文件 将标准输出追加重定向到文件 将错误输出重定向到文件 将标准输出和错误输出都重定向到文件 将错误输出重定向到黑洞文件 输入重定向 重定向的分类 名 ...

  10. IDE - vscode

    [一]VSCODE官方插件库 https://marketplace.visualstudio.com/ 最好能在文件->首选项->设置中,搜索update,将Auto Update关闭, ...