ASP.NET Core MVC MySQL Identity

本文关键字:MySQL Identity MVC Core NET ASP | 更新日期: 2023-09-27 18:08:11

我使用第三方MySQL EntityFrameworkCore扩展

" 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
                );
            });

谢谢

ASP.NET Core MVC MySQL Identity

这是MySQL连接器的一个限制,已经在7.1.9中解决了

你可以在这里报告类似的问题

这个项目似乎有一个更完整的迁移实现:

https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql