使用EF的Code First模式创建模型
Entity Framework Core
Entity Framework (EF) Core 是轻量化、可扩展、开源和跨平台版的常用 Entity Framework 数据访问技术。
EF Core 可用作对象关系映射程序 (O/RM),这可以实现以下两点:
使 .NET 开发人员能够使用 .NET 对象处理数据库。
无需再像通常那样编写大部分数据访问代码。
EF 支持以下模型开发方法:
- Database First:先有数据库,后有实体模型。从现有数据库生成模型。对模型手动编码,使其符合数据库。
- Code First:创建模型后,使用 EF 迁移从模型创建数据库。 模型发生变化时,迁移可让数据库不断演进。先有实体模型,后生成数据库
创建模型
通用模型
数据表,都会有主键字段。为了满足此需求,所以我们建立一个通用的泛型模型BaseEntity
/// <summary>
/// 通用基本模型
/// </summary>
/// <typeparam name="K">主键类型</typeparam>
public class BaseEntity<K>
{
/// <summary>
/// Id,主键
/// </summary>
public K Id { get; set; }
}
/// <summary>
/// 通用模型
/// </summary>
public class BaseEntity : BaseEntity<string>
{
}
为了让系统知道我们的Id字段是主键,我们增加了对通用模型的配置
EF可以有两种方法来配置实体,一种是使用数据批注(Attribute)
还有一种是Fluent API,就是通过代码来对模型配置
本示例使用Fluent API来配置
因为有些特殊情况是不能使用数据批注(Attribute)来满足要求
比如多主键,多外键,关联等情况。
/// <summary>
/// 默认实体配置
/// OnModelCreating
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="K"></typeparam>
public class BaseEntityTypeConfig<T, K> : IEntityTypeConfiguration<T>
where T : BaseEntity<K>
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
#region 主外键关系
builder.HasKey(k => k.Id);//设置主键
#endregion
#region 字段属性:最大长度,是否必需,默认值
#endregion
#region 备注
builder.Property(p => p.Id).HasComment("主键");//设置备注
#endregion
}
}
public class BaseEntityTypeConfig<T> : BaseEntityTypeConfig<T, string>, IEntityTypeConfiguration<T>
where T : BaseEntity
{
public override void Configure(EntityTypeBuilder<T> builder)
{
base.Configure(builder);
#region 主外键关系
#endregion
#region 字段属性:最大长度,是否必需,默认值
builder.Property(p => p.Id).HasMaxLength(50);//设置主键最大长度50
#endregion
#region 备注
#endregion
}
}
对于业务实体,一般我们又会有些其它通过字段,比如创建人,创建时间,修改人,修改时间,是否删除等公共字段
所以我们创建了BusEntity通用业务模型
/// <summary>
/// 业务实体基类
/// </summary>
/// <typeparam name="K">主键类型</typeparam>
public class BusEntity<K> : BaseEntity<K>
{
/// <summary>
/// 是否删除
/// </summary>
public bool Deleted { get; set; }
/// <summary>
/// 创建人
/// </summary>
public K CreateUserId { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
/// 修改人
/// </summary>
public K ModifyUserId { get; set; }
/// <summary>
/// 修改时间
/// </summary>
public DateTime ModifyTime { get; set; }
}
/// <summary>
/// 业务实体基类
/// </summary>
public class BusEntity : BusEntity<string>
{ }
对于基本业务实体基类用以下配置
/// <summary>
/// 默认实体配置
/// OnModelCreating
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="K"></typeparam>
public class BusEntityTypeConfig<T, K> : BaseEntityTypeConfig<T, K>, IEntityTypeConfiguration<T>
where T : BusEntity<K>
{
public override void Configure(EntityTypeBuilder<T> builder)
{
base.Configure(builder);
builder.HasQueryFilter(q => q.Deleted == false);//查询自动过滤已经删除的记录
#region 主外键关系
#endregion
#region 字段属性:最大长度,是否必需,默认值
builder.Property(p => p.Deleted).HasDefaultValue(false);//把是否删除设置为默认False
builder.Property(p => p.CreateUserId).HasMaxLength(50);//把创建人设置为默认值
builder.Property(p => p.ModifyUserId).HasMaxLength(50);//把修改人设置为默认值
builder.Property(p => p.CreateTime).HasDefaultValueSql("getdate()").ValueGeneratedOnAdd();//把创建时间设置默认值并在增加的时候更新值
builder.Property(p => p.ModifyTime).HasDefaultValueSql("getdate()").ValueGeneratedOnAddOrUpdate();//把修改时间设置默认值并在增加和修改的时候更新值
#endregion
#region 备注
builder.Property(p => p.Deleted).HasComment("是否删除");
builder.Property(p => p.CreateUserId).HasComment("创建人");
builder.Property(p => p.CreateTime).HasComment("创建时间");
builder.Property(p => p.ModifyUserId).HasComment("修改人");
builder.Property(p => p.ModifyTime).HasComment("修改时间");
#endregion
}
}
public class BusEntityTypeConfig<T> : BusEntityTypeConfig<T, string>, IEntityTypeConfiguration<T>
where T : BusEntity
{
public override void Configure(EntityTypeBuilder<T> builder)
{
base.Configure(builder);
#region 主外键关系
#endregion
#region 字段属性:最大长度,是否必需,默认值
builder.Property(p => p.Id).HasMaxLength(50);
#endregion
#region 备注
#endregion
}
}
业务模型
接下来我们有了通用模型基类。那么我们就可以来创建具体的业务模型
比如说我们的组织架构模型
我们使用两个局部类来定义,
第一个局部类定义基本属性
第二个局部类定义关联关系
然后对模型进行配置
/// <summary>
/// 组织架构
/// </summary>
public partial class Sys_Org : BusEntity
{
/// <summary>
/// 上级组织
/// </summary>
public string ParentId { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
}
public partial class Sys_Org : BusEntity
{
/// <summary>
/// 上级组织
/// </summary>
public Sys_Org Parent { get; set; }
/// <summary>
/// 下级组织
/// </summary>
public List<Sys_Org> Childs { get; set; }
}
/// <summary>
/// 实体配置
/// OnModelCreating
/// </summary>
public class Sys_OrgTypeConfig : BusEntityTypeConfig<Sys_Org>, IEntityTypeConfiguration<Sys_Org>
{
public override void Configure(EntityTypeBuilder<Sys_Org> builder)
{
base.Configure(builder);
#region 主外键关系
builder.HasOne(p => p.Parent).WithMany(p => p.Childs).HasForeignKey(p => p.ParentId);
#endregion
#region 字段属性:最大长度,是否必需,默认值
builder.Property(p => p.Name).HasMaxLength(50).IsRequired();
#endregion
#region 备注
builder.Property(p => p.ParentId).HasComment("上级组织");
builder.Property(p => p.Name).HasComment("名称");
#endregion
}
}
/// <summary>
/// 系统用户
/// </summary>
public partial class Sys_User : BusEntity
{
/// <summary>
/// 工号、编码
/// </summary>
public string Code { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 状态
/// </summary>
public string Status { get; set; }
/// <summary>
/// 所属组织
/// </summary>
public string OrgId { get; set; }
/// <summary>
/// 性别
/// </summary>
public string Sex { get; set; }
}
public partial class Sys_User : BusEntity
{
/// <summary>
/// 所属组织
/// </summary>
public Sys_Org Org { get; set; }
}
/// <summary>
/// 实体配置
/// OnModelCreating
/// </summary>
public class Sys_UserTypeConfig : BusEntityTypeConfig<Sys_User>, IEntityTypeConfiguration<Sys_User>
{
public override void Configure(EntityTypeBuilder<Sys_User> builder)
{
base.Configure(builder);
#region 主外键关系
builder.HasOne(p => p.Org).WithMany().HasForeignKey(p => p.OrgId);
#endregion
#region 字段属性:最大长度,是否必需,默认值
builder.Property(p => p.Code).HasMaxLength(50);
builder.Property(p => p.Name).HasMaxLength(50).IsRequired();
builder.Property(p => p.UserName).HasMaxLength(50).IsRequired();
builder.Property(p => p.Password).HasMaxLength(100).IsRequired();
builder.Property(p => p.Status).HasMaxLength(50).IsRequired();
builder.Property(p => p.OrgId).HasMaxLength(50);
builder.Property(p => p.Sex).HasMaxLength(50);
#endregion
#region 备注
builder.Property(p => p.Code).HasComment("编码");
builder.Property(p => p.Name).HasComment("名称");
builder.Property(p => p.UserName).HasComment("用户名");
builder.Property(p => p.Password).HasComment("密码");
builder.Property(p => p.Status).HasComment("状态");
builder.Property(p => p.OrgId).HasComment("所属组织");
builder.Property(p => p.Sex).HasComment("性别");
#endregion
}
}
创建数据库上下文Context
有了数据模型,接下来我们创建数据库上下文Context
OnConfiguring用来配置数据连接字符串
OnModelCreating是创建模型是对模型的配置
因为上面我们对每个模型都做了配置(实现了IEntityTypeConfiguration)
所以在这里我们只要把具体的配置配置应用到Context就行了
我们使用modelBuilder.ApplyConfigurationsFromAssembly
就可以把所有实现了IEntityTypeConfiguration的模型配置全部应用到数据库上下文
public class GDbContext : DbContext
{
/// <summary>
/// Context配置
/// </summary>
/// <param name="optionsBuilder"></param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Data Source=x.x.x.x;Initial Catalog=数据库名称;User Id=用户名;Password=密码;APP=系统名称;Pooling=true;");
}
/// <summary>
/// 模型创建
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
/// <summary>
/// 组织架构
/// </summary>
public DbSet<Sys_Org> Sys_Org { get; set; }
/// <summary>
/// 用户
/// </summary>
public DbSet<Sys_User> Sys_User { get; set; }
}
所有代码结构如下

迁移数据库
专业名称叫迁移,通用解释就是把实体模型生成为数据表
我们在OnConfiguring中已经配置了使用SqlServer数据库(当然也支持其它所有类型数据库:如MySql,Oracle,Sqlite等其它数据库)
使用迁移,必需安装Microsoft.EntityFrameworkCore.Tools工具集
接下来我们在“程序包管理控制台”输入Add-Migration FirstInit (FirstInit:自定义名称)
系统会自动帮我们生成迁移代码


public partial class FirstInit : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Sys_Org",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "主键"),
ParentId = table.Column<string>(type: "nvarchar(50)", nullable: true, comment: "上级组织"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "名称"),
Deleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "是否删除"),
CreateUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "创建人"),
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "创建时间"),
ModifyUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "修改人"),
ModifyTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "修改时间")
},
constraints: table =>
{
table.PrimaryKey("PK_Sys_Org", x => x.Id);
table.ForeignKey(
name: "FK_Sys_Org_Sys_Org_ParentId",
column: x => x.ParentId,
principalTable: "Sys_Org",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "Sys_User",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "主键"),
Code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "编码"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "名称"),
UserName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "用户名"),
Password = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, comment: "密码"),
Status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "状态"),
OrgId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "所属组织"),
Sex = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "性别"),
Deleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "是否删除"),
CreateUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "创建人"),
CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "创建时间"),
ModifyUserId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "修改人"),
ModifyTime = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "getdate()", comment: "修改时间")
},
constraints: table =>
{
table.PrimaryKey("PK_Sys_User", x => x.Id);
table.ForeignKey(
name: "FK_Sys_User_Sys_Org_OrgId",
column: x => x.OrgId,
principalTable: "Sys_Org",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Sys_Org_ParentId",
table: "Sys_Org",
column: "ParentId");
migrationBuilder.CreateIndex(
name: "IX_Sys_User_OrgId",
table: "Sys_User",
column: "OrgId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Sys_User");
migrationBuilder.DropTable(
name: "Sys_Org");
}
}
更新到数据库
我们通过Update-Database命令更新到数据库


生成SQL脚本更新到生产数据库
有时候我们系统已经在运行了。开始也不可能直接连接生产数据库
那么我们修改了模型,可以通过生成SQL脚本,来更新数据库
#起始迁移点(没有写0)
#结束迁移点
Script-Migration -From 0 -To 20210225022927_FirstInit

IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
GO
BEGIN TRANSACTION;
GO
CREATE TABLE [Sys_Org] (
[Id] nvarchar(50) NOT NULL,
[ParentId] nvarchar(50) NULL,
[Name] nvarchar(50) NOT NULL,
[Deleted] bit NOT NULL DEFAULT CAST(0 AS bit),
[CreateUserId] nvarchar(50) NULL,
[CreateTime] datetime2 NOT NULL DEFAULT (getdate()),
[ModifyUserId] nvarchar(50) NULL,
[ModifyTime] datetime2 NOT NULL DEFAULT (getdate()),
CONSTRAINT [PK_Sys_Org] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Sys_Org_Sys_Org_ParentId] FOREIGN KEY ([ParentId]) REFERENCES [Sys_Org] ([Id]) ON DELETE NO ACTION
);
DECLARE @defaultSchema AS sysname;
SET @defaultSchema = SCHEMA_NAME();
DECLARE @description AS sql_variant;
SET @description = N'主键';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'Id';
SET @description = N'上级组织';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'ParentId';
SET @description = N'名称';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'Name';
SET @description = N'是否删除';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'Deleted';
SET @description = N'创建人';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'CreateUserId';
SET @description = N'创建时间';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'CreateTime';
SET @description = N'修改人';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'ModifyUserId';
SET @description = N'修改时间';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_Org', 'COLUMN', N'ModifyTime';
GO
CREATE TABLE [Sys_User] (
[Id] nvarchar(50) NOT NULL,
[Code] nvarchar(50) NULL,
[Name] nvarchar(50) NOT NULL,
[UserName] nvarchar(50) NOT NULL,
[Password] nvarchar(100) NOT NULL,
[Status] nvarchar(50) NOT NULL,
[OrgId] nvarchar(50) NULL,
[Sex] nvarchar(50) NULL,
[Deleted] bit NOT NULL DEFAULT CAST(0 AS bit),
[CreateUserId] nvarchar(50) NULL,
[CreateTime] datetime2 NOT NULL DEFAULT (getdate()),
[ModifyUserId] nvarchar(50) NULL,
[ModifyTime] datetime2 NOT NULL DEFAULT (getdate()),
CONSTRAINT [PK_Sys_User] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Sys_User_Sys_Org_OrgId] FOREIGN KEY ([OrgId]) REFERENCES [Sys_Org] ([Id]) ON DELETE NO ACTION
);
DECLARE @defaultSchema AS sysname;
SET @defaultSchema = SCHEMA_NAME();
DECLARE @description AS sql_variant;
SET @description = N'主键';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Id';
SET @description = N'编码';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Code';
SET @description = N'名称';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Name';
SET @description = N'用户名';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'UserName';
SET @description = N'密码';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Password';
SET @description = N'状态';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Status';
SET @description = N'所属组织';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'OrgId';
SET @description = N'性别';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Sex';
SET @description = N'是否删除';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'Deleted';
SET @description = N'创建人';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'CreateUserId';
SET @description = N'创建时间';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'CreateTime';
SET @description = N'修改人';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'ModifyUserId';
SET @description = N'修改时间';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'Sys_User', 'COLUMN', N'ModifyTime';
GO
CREATE INDEX [IX_Sys_Org_ParentId] ON [Sys_Org] ([ParentId]);
GO
CREATE INDEX [IX_Sys_User_OrgId] ON [Sys_User] ([OrgId]);
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20210225022927_FirstInit', N'5.0.3');
GO
COMMIT;
GO
这样的话。就只要把这个脚本放到生产环境去运行,
那么得到的结果也是和Update-Database结果是一样的
使用EF的Code First模式创建模型的更多相关文章
- (转载)EF 使用code first模式创建数据库和 填充种子数据
第一篇:来自 .net 开发菜鸟 博主的文章:https://www.cnblogs.com/dotnet261010/p/8035213.html 第二篇:来自 JustYong 博主的文章:htt ...
- EntityFramework使用Code First模式创建数据库控制生成单数形式的表名
使用Code-First模式生成数据库时,默认生成的数据库表的名称为类型的复数形式,例如实体类名称是"User",默认生成的数据库表名为“Users”,多数情况下我们并不想生成的数 ...
- 使用MVC5+Entity Framework6的Code First模式创建数据库并实现增删改查功能
此处采用VS2017+SqlServer数据库 一.创建项目并引用dll: 1.创建一个MVC项目 2.采用Nuget安装EF6.1.3 二.创建Model 在models文件夹中,建立相应的mode ...
- EF应用一:Code First模式
EF的核心程序集位于System.Data.Entity.dll和System.Data.EntityFramework.dll中.支持CodeFirst的位于EntityFramework.dll中 ...
- 8天掌握EF的Code First开发系列之2 Code First开发系列之领域建模和管理实体关系
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 理解Code First及其约定和配置 创建数据表结构 管理实体关系 三种继承模式 本章小结 本人的实验环境是V ...
- Entity Framework应用:使用Code First模式管理视图
一.什么是视图 视图在RDBMS(关系型数据库管理系统)中扮演了一个重要的角色,它是将多个表的数据联结成一种看起来像是一张表的结构,但是没有提供持久化.因此,可以将视图看成是一个原生表数据顶层的一个抽 ...
- [EF] - 动态创建模型:System.Reflection.Emit + Code First
动态创建Entity Framework模型并且创建数据库 使用System.Reflection.Emit+Code First model创建以下的一个实体类和DbContext并且创建数据库: ...
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...
- EF 中 Code First 的数据迁移以及创建视图
写在前面: EF 中 Code First 的数据迁移网上有很多资料,我这份并没什么特别.Code First 创建视图网上也有很多资料,但好像很麻烦,而且亲测好像是无效的方法(可能是我太笨,没搞成功 ...
随机推荐
- Deep Learning论文翻译(Nature Deep Review)
原论文出处:https://www.nature.com/articles/nature14539 by Yann LeCun, Yoshua Bengio & Geoffrey Hinton ...
- C++隐式推导-auto关键词
总述 C++中有一个关键字,它不进行显式声明,而进行隐式推导,auto可以在声明变量时根据变量初始值的类型自动为此变量选择匹配的类型.C++语言类似的关键字还有decltype. 如何评价 C++ 1 ...
- Spring 的循环依赖,源码详细分析 → 真的非要三级缓存吗
开心一刻 吃完晚饭,坐在院子里和父亲聊天 父亲:你有什么人生追求? 我:金钱和美女 父亲对着我的头就是一丁弓,说道:小小年纪,怎么这么庸俗,重说一次 我:事业与爱情 父亲赞赏的摸了我的头,说道:嗯嗯, ...
- Java线程八锁
package com.atguigu.juc1205; import java.util.concurrent.TimeUnit; class Phone//Phone.java ---> P ...
- URAL - 1029 dp
题意: n层楼,每层楼有m个房间.找出一个路径从第一层到达第M层,使得路径上的所有数的和是所有可达路径中最小的,每次上到下一层以后就不能再上去,依次输出路径上的各点在所在层的列数. 题解: 参考链接: ...
- 牛客编程巅峰赛S1第11场 - 黄金&钻石 C.牛牛找子集 (二分)
题意:有一\(n\)个数,从中找数构成相同的子集,要求子集元素个数为\(k\),求构成子集个数最多的情况,输出子集(字典序最小). 题解:我们可以对子集的个数二分答案,首先用桶记录每个元素的个数,然后 ...
- Bootstrap巨幕
这是一个轻量.灵活的组件,它能延伸至整个浏览器视口来展示网站上的关键内容. jumbotron修饰 <div class="jumbotron"> <h1> ...
- 51nod 1384 可重集的全排列
对于1231,121,111等有重复的数据,我们怎么做到生成全排列呢 实际上,对于打标记再释放标记的这种方法,如果一开始第一层递归访问过1那么你再访问 就会完全重复上一次1开头的情况,那么递归地考虑这 ...
- 北京网络赛G BOXES 大模拟+BFS
题目描述 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxe ...
- Web 前端如何一键开启上帝模式
Web 前端如何一键开启上帝模式 God Mode document.designMode = `on`; refs https://www.cnblogs.com/xgqfrms/tag/desig ...