dotnet core 2.0 发布已经好几天了,期间也把原来 dotnet core 1.1 的 MVC 项目升级到了 2.0,升级过程还是比较顺利的,变动也不是太多。升级的过程中也少不了 Entity Framwork Core 的升级,在这篇文章中主要介绍下 MySql 数据库使用 Entity Framwork Core 2.0 如何进行 Code First、Database First 及数据库迁移(Migration),虽然比较基础,但是做到尽量详细。本文所有示例代码已经提交到 GitHub:https://github.com/starts2000/EFCoreDemo,如果觉得对你有帮助,就给个 Star 吧。
public class User { public int Id { get; set; } [MaxLength(32), Required] public string Aaccount { get; set; } [MaxLength(32), Required] public string Password { get; set; } }
最终【Starts2000.EFCoreCodeFirst】项目结构如下:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
最终内容如下:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <ApplicationIcon /> <OutputType>Exe</OutputType> <StartupObject /> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\Starts2000.EFCoreCodeFirst\Starts2000.EFCoreCodeFirst.csproj" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.0-rtm-10058" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup> </Project>
public class TestDbContext : DbContext { public DbSet<User> User { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySql( "server=localhost;database=TestDb;user=test;password=123456;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //modelBuilder.Entity<User>().HasIndex(u => u.Aaccount).IsUnique(); } }
dotnet ef migrations add InitialCreate
如果命令执行成功,可以看到项目目录中新增了如下内容:
dotnet ef database update
命令执行成功后,可以看到数据库及表创建成功。
User 类的 Account 属性对应 User 表的 Account 列应该是唯一的,前面忘记了设置唯一索引,现在在 TestDbContext 中加上:
public class TestDbContext : DbContext { public DbSet<User> User { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySql( "server=localhost;database=TestDb;user=test;password=123456;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<User>().HasIndex(u => u.Aaccount).IsUnique(); } }
执行 dotnet ef migrations add UserTableUpdateAccount
命令:
如果命令执行成功,可以看到项目目录中新增了如下内容:
接着按照 步骤7 的操作,如果成功,可以看到 User 表已经创建了 Account 的唯一索引:
在 Main 函数中添加如下代码:
static void Main(string[] args) { using(var context = new TestDbContext()) { context.User.Add(new Models.User { Aaccount = "CodeFirst-Test-" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), Password = "123456" }); context.SaveChanges(); Console.WriteLine(context.User .OrderByDescending(u => u.Id) .FirstOrDefault()?.Aaccount); } Console.ReadKey(); }
把【Starts2000.EFCoreCodeFirst.Test】项目设置为启动项目,编译运行:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
最终内容如下:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.0-rtm-10057" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup> </Project>
dotnet ef dbcontext scaffold "Server=localhost;User Id=test;Password=123456;Database=TestDb" "Pomelo.EntityFrameworkCore.MySql"
执行成功后,项目增加了如下内容:
static void Main(string[] args) { using (var context = new TestDbContext()) { context.User.Add(new User { Aaccount = "DbFirst-Test-" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), Password = "123456" }); context.SaveChanges(); Console.WriteLine(context.User .OrderByDescending(u => u.Id) .FirstOrDefault()?.Aaccount); } Console.ReadKey(); }
参考文章: