我使用的是第三方的MySQL EntityFrameworkCore擴展ASP.NET MVC核心MySQL的身份
「SapientGuardian.EntityFrameworkCore.MySql」: 「7.1.7」
一切正常,但現在我決定用AspNetCore身份框架,所以我改變了我的DbContext到IdentityDbContext:
public class TwitchCharDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
public TwitchCharDbContext (DbContextOptions<TwitchCharDbContext> options)
: base(options)
{ }
}
當我運行命令來更新數據庫:
dotnet ef migrations add init
dotnet ef database update
我得到這個錯誤:
System.InvalidOperationException: No mapping to a relational type can be found for the CLR type 'Microsoft.EntityFrameworkCore.Metadata.Internal.Property'
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapperExtensions.GetMapping(IRelationalTypeMapper typeMapper, IProperty property)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.GetColumnType(String schema, String table, String name, Type clrType, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, IModel model)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, Type clrType, String type, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, Boolean nullable, Object defaultValue, String defaultValueSql, String computedColumnSql, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder)
at MySQL.Data.Entity.Migrations.MySQLMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, Type clrType, String type, Nullable`1 unicode, Nullable`1 maxLength, Boolean rowVersion, Boolean nullable, Object defaultValue, String defaultValueSql, String computedColumnSql, IAnnotatable annotatable, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(CreateTableOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
No mapping to a relational type can be found for the CLR type 'Microsoft.EntityFrameworkCore.Metadata.Internal.Property'
重要注意的是,如果我在Startup.cs改變目標數據庫MS SQL它按預期工作。
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(30);
options.Cookies.ApplicationCookie.SlidingExpiration = true;
options.Cookies.ApplicationCookie.LoginPath = new PathString("/auth/sign-in");
}).AddEntityFrameworkStores<TwitchCharDbContext, int>();
services.AddDbContext<TwitchCharDbContext>(opt => {
//opt.UseSqlServer(Configuration.GetConnectionString("TwitchChar"));
opt.UseMySQL(
connectionString
);
});
謝謝
完美!它解決了我試圖更新的問題,但nuget提出了我最高版本的7.1.7。無論如何,我還是放了7.1.9,併成功恢復了項目。謝謝。 – tomaspavlic