源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

在.NET Core类库中使用EF Core迁移数据库到SQL Server的方法

  • 时间:2022-06-08 13:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:在.NET Core类库中使用EF Core迁移数据库到SQL Server的方法
[b]前言[/b] 如果大家刚使用EntityFramework Core作为ORM框架的话,想必都会遇到数据库迁移的一些问题。 起初我是在ASP.NET Core的Web项目中进行的,但后来发现放在此处并不是很合理,一些关于数据库的迁移,比如新增表,字段,修改字段类型等等,不应该和最上层的Web项目所关联,数据的迁移文件放到这里也感觉有点多余,有点乱乱的感觉,所以才想着单独出来由专门的项目进行管理会比较好,也比较清晰! 注意目标框架选择的是.NET Core 2.0而不是.NET Standard 2.0 [b]0、前期准备[/b] a)、表实体定义,这个是在.NET Standard 2.0的类库中存放的。
/// <summary>
 /// 系统应用的用户实体
 /// </summary>
 public class ApplicationUser : BaseModel
 {
  /// <summary>
  /// 用户名
  /// </summary>
  public string UserName { get; set; }
  /// <summary>
  /// 密码
  /// </summary>
  public string Password { get; set; }
  /// <summary>
  /// 邮件地址
  /// </summary>
  public string Email { get; set; }
 }
b)、新建一个.NET Core 2.0的类库,并定义好我们所要使用的数据库上下文,很简单,接下来开始我们的正文
/// <summary>
 /// 系统上下文
 /// </summary>
 public class LightContext : DbContext
 {
  public LightContext(DbContextOptions<LightContext> options) : base(options)
  {
  }
  /// <summary>
  /// 系统应用用户
  /// </summary>
  public DbSet<ApplicationUser> ApplicationUser { get; set; }
  /// <summary>
  /// 角色表
  /// </summary>
  public DbSet<Role> Role { get; set; }
 }
[b]1、问题汇总[/b] 首先要确保仓储类库中已经引入以下两个Nuget包,没有的话请使用包管理器进行安装。不建议直接引入原包:Microsoft.AspNetCore.All,按需引入即可
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
a)打开CMD,然后切换到类库所在路径下,执行以下命令。不过你也可以使用程序包管理器控制台(PMC)进行迁移,但是会有少许变化,部分命令见下表:
迁移命令描述 CMD命令 PMC命令
创建迁移:migrationname为迁移名称 dotnet ef migrations add migrationname add-migration migrationname
移除迁移(删除最近的一次迁移) dotnet ef migrations remove remove-migration
应用最新的迁移(使迁移文件应用到数据库) dotnet ef database update update-database
应用指定的迁移 dotnet ef database update migrationname update-database migrationname
查看迁移列表 dotnet ef migrations list
查看数据库上下文信息 dotnet ef dbcontext info
dotnet ef
[img]http://files.jb51.net/file_images/article/201712/2017120809183619.png[/img] 错误提示: 未找到与命令“dotnet-ef”匹配的可执行文件 [b]解决方法:[/b] 在项目文件Light.Repository.csproj中添加以下节点
<ItemGroup>
 <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
重新执行上面的命令,如果出现了EF Core的标志(一头蓄势待发的野马)表示已经成功 [img]http://files.jb51.net/file_images/article/201712/2017120809183620.png[/img] b)、执行以下命令进行迁移
dotnet ef migrations add InitLightDB
[img]http://files.jb51.net/file_images/article/201712/2017120809183621.png[/img] 错误提示: The specified framework version '2.0' could not be parsed The specified framework 'Microsoft.NETCore.App', version '2.0' was not found. - Check application dependencies and target a framework version installed at: \ - Alternatively, install the framework version '2.0'. [b]解决方法:[/b] 在项目文件中添加以下节点:
<PropertyGroup>
 <TargetFramework>netcoreapp2.0</TargetFramework>
 <RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>
 </PropertyGroup>
c)、重新执行b步骤的命令,报错信息如下: [img]http://files.jb51.net/file_images/article/201712/2017120809183622.png[/img] [b]错误提示:[/b] Unable to create an object of type 'LightContext'. Add an implementation of 'IDesignTimeDbContextFactory<LightContext>' to the project, or see[b][url=https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db]https://go.microsoft.com/fwlink/?linkid=851728[/url][/b] for additional patterns supported at design time. 这个问题如果是在Web项目,并且配置了DbContext的链接字符串的话,是不会出现此问题的。很显然是迁移命令没有找到DbConnectionString导致的,接下来我们按照提示,实现一个IDesignTimeDbContextFactory<LightContext>试试 [b]解决方法:[/b] 创建一个与DbContext同一目录下的DesignTimeDbContextFactory文件,然后实现接口中的方法CreateDbContext,并配置ConnectionString
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<LightContext>
 {
  public LightContext CreateDbContext(string[] args)
  {
   var builder = new DbContextOptionsBuilder<LightContext>();
   builder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Initial Catalog=Light;");
   return new LightContext(builder.Options);
  }
 }
再次执行迁移命令,终于成功了。 [img]http://files.jb51.net/file_images/article/201712/2017120809183623.png[/img] [b]成功提示:[/b] Done. To undo this action, use 'ef migrations remove' 同时类库下面会生成Migrations文件夹以及相关的迁移文件 [img]http://files.jb51.net/file_images/article/201712/2017120809183624.png[/img] [b]2、小试迁移命令[/b] a)、使用以下命令应用迁移,生成数据库和表
dotnet ef database update
[img]http://files.jb51.net/file_images/article/201712/2017120809183625.png[/img] 通过VS的SQL Server资源管理器查看生成数据库的结构,其中__EFMigrationsHistory为每次迁移的记录表 [img]http://files.jb51.net/file_images/article/201712/2017120809183626.png[/img] b)、因为string类型的字段迁移到数据库之后的数据类型为nvarchar(max)并且是可空类型的,下面我们就使用Fluent API对ApplicationUser表字段进行配置,同样你也可以使用属性注解的方式进行配置,因为我自己不喜欢“污染”表实体
public static void ConfigApplicationUser(ModelBuilder modelBuilder)
  {
   modelBuilder.Entity<ApplicationUser>(m =>
   {
    m.Property(t => t.Email)
      .HasMaxLength(50);
    m.Property(t => t.UserName)
      .IsRequired()
      .HasMaxLength(50);
    m.Property(t => t.Password)
      .IsRequired()
      .HasMaxLength(20);
   });
  }
然后同样使用上面的两条命令重新迁移并更新数据库结构 [img]http://files.jb51.net/file_images/article/201712/2017120809183627.png[/img] 观察数据库表结构已经更新 [img]http://files.jb51.net/file_images/article/201712/2017120809183628.png[/img] 同理添加字段,删除字段都是一样的迁移操作,还是很方便的 [b]3、扩展[/b] a)、为了方便演示,其实上面在类库中执行迁移时的数据库连接字符串是写死的,那么最好的办法是应该去读取Web项目下已经配置好的连接,这样就能保证上下的一致性,不用再去为了EF的迁移而单独维护一个多余的数据库连接配置。改造也很简单,即通过Configuration组件读取appsettings.json的ConnectionStrings节点,改造之后是这样子的:
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<LightContext>
 {
  public LightContext CreateDbContext(string[] args)
  {
   Directory.SetCurrentDirectory("..");//设置当前路径为当前解决方案的路径
   string appSettingBasePath = Directory.GetCurrentDirectory() + "/Light.AuthorityApi";//改成你的appsettings.json所在的项目名称
   var configBuilder = new ConfigurationBuilder()
    .SetBasePath(appSettingBasePath)
    .AddJsonFile("appsettings.json")
    .Build();
   var builder = new DbContextOptionsBuilder<LightContext>();
   //builder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Initial Catalog=Light;");
   builder.UseSqlServer(configBuilder.GetConnectionString("LightConnection"));
   return new LightContext(builder.Options);
  }
 }
注意需要额外引入下面这个Nuget包:
Install-Package Microsoft.Extensions.Configuration.Json
b)、属性注解[Column(Order = 1)]对EF Core来说还没有达到可以调整数据库生成字段的顺序,不过我们还是可以修改迁移文件的实体属性的顺序来达到我们想要的效果。下面是我调整之后重新生成的表,是不是看出来和上面的有什么不同,一图胜万语: [img]http://files.jb51.net/file_images/article/201712/2017120809183629.png[/img] c)、最后一步,自己动手试试看:创建一个SeedData迁移文件来添加数据库的初始数据。:) [b]4、最后[/b] EF Core的强大远不止这些,还有更多的使用方法等着我们去发现,去探索。每天进步一点点,是件很愉快的事情!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部