使用ASP.NET核心和MySQL从数据库优先迁移到具有标识的代码优先模型
我开发一个网站已经有一段时间了。最近我意识到我需要一个数据库和身份模型(使用ASP.NET的内置功能),因为我想先将数据库模型转换为代码模型 现在,我意识到这在结构上是一个相当剧烈的变化,但我认为这应该是可能的 结构 我已经使用了以下软件包来准备迁移和转换:使用ASP.NET核心和MySQL从数据库优先迁移到具有标识的代码优先模型,mysql,ef-code-first,asp.net-core-mvc,entity-framework-core,asp.net-core-identity,Mysql,Ef Code First,Asp.net Core Mvc,Entity Framework Core,Asp.net Core Identity,我开发一个网站已经有一段时间了。最近我意识到我需要一个数据库和身份模型(使用ASP.NET的内置功能),因为我想先将数据库模型转换为代码模型 现在,我意识到这在结构上是一个相当剧烈的变化,但我认为这应该是可能的 结构 我已经使用了以下软件包来准备迁移和转换: "Microsoft.AspNetCore.Identity": "1.0.0", "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0", "SapientGuardian
"Microsoft.AspNetCore.Identity": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"SapientGuardian.EntityFrameworkCore.MySql": "7.1.4",
"Microsoft.EntityFrameworkCore.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0"
我发现这是必须的(各种文章、博客帖子等等)
从这里,我设置了我的上下文DatabaseContext
,如下所示:
public class DatabaseContext : IdentityDbContext<User>
{
public DatabaseContext(DbContextOptions<DatabaseContext> options)
:base(options)
{ }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
<!-- I'm not interested in using all IdentityUser properties, hence ignoring those not needed -->
builder.Entity<User>().ToTable("Users")
.Ignore(x => x.ConcurrencyStamp)
.Ignore(x => x.LockoutEnabled)
.Ignore(x => x.LockoutEnd)
.Ignore(x => x.NormalizedEmail)
.Ignore(x => x.NormalizedUserName)
.Ignore(x => x.PhoneNumber)
.Ignore(x => x.PhoneNumberConfirmed)
.Ignore(x => x.TwoFactorEnabled);
<!-- Again, ignoring the properties I do not want from IdentityRole -->
builder.Entity<IdentityRole>().ToTable("Roles")
.Ignore(x => x.ConcurrencyStamp)
.Ignore(x => x.NormalizedName);
<!-- Adding the rest of the identity models, and mapping these into their correct database table -->
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
}
public DbSet<BlogPost> BlogPosts { get; set; }
<!-- etc... -->
}
问题
在做了这些更改之后,我会假设所有的设置都是正确的(如果遗漏了什么,请告诉我)。从这里,我可以通过运行以下命令来创建我的init迁移:
dotnet ef migrations add init
dotnet ef database update
我可以看到一个Migrations
文件夹被添加到我的项目中
问题1:它不应该在我的迁移文件夹中创建一个Configuration.cs
类,在那里我可以为数据库种子
我还假设我应该能够使用以下命令更新数据库:
dotnet ef migrations add init
dotnet ef database update
问题2:但这给了我以下错误:
对象引用未设置为对象的实例
堆栈跟踪:
System.NullReferenceException:对象引用未设置为对象的实例。
在MySQL.Data.Entity.Migrations.MySQLHistoryRepository.get_ExistsSql()上
在Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()中
位于Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(字符串targetMigration)
位于Microsoft.EntityFrameworkCore.Design.MigrationOperations.UpdateDatabase(String targetMigration,String contextType)
在Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.c__DisplayClass0_0.b_0()中
位于Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(字符串[]args)
位于Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(字符串[]args)
问题
这是我第一次在ASP.NET Core中对MySQL服务器使用EF代码,这意味着我基本上是在没有足够知识的情况下使用它。迁移种子已在EF Core中删除。请参阅。迁移种子已在EF Core中删除。看见