1

我想创建一个迁移来在我的数据库中创建一个新表,但是在执行之后Add-Migration MsSql_Add_ArticleCategory_Table,我得到了这个错误:

对于 CLR 类型为“Nullable”的属性“Alpha.Models.Identity.User.LockoutEnd”,找不到到关系类型的映射。

我该如何解决它(我在我的项目中使用了 asp.net core 3.1)?

PM> Add-Migration MsSql_Add_ArticleCategory_Table 构建开始...构建成功。Microsoft.EntityFrameworkCore.Model.Validation[30000] 没有为实体类型“Article”上的十进制列“RateCounter”指定类型。如果值不符合默认精度和比例,这将导致值被静默截断。使用“HasColumnType()”显式指定可以容纳所有值的 SQL 服务器列类型。Microsoft.EntityFrameworkCore.Model.Validation[30000] 没有为实体类型“Rating”的十进制列“Rate”指定类型。如果值不符合默认精度和比例,这将导致值被静默截断。使用“HasColumnType()”显式指定可以容纳所有值的 SQL 服务器列类型。Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 3.1.3 使用提供程序“Microsoft.EntityFrameworkCore.SqlServer”初始化“ApplicationDbContext”,选项:MigrationsAssembly=Alpha.DataAccess System.InvalidOperationException:找不到属性的关系类型的映射CLR 类型为“Nullable”的“Alpha.Models.Identity.User.LockoutEnd”。在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IProperty 源,IProperty 目标,DiffContext diffContext)+MoveNext() 在 Microsoft.EntityFrameworkCore.Migrations 的 Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty 属性)。 Internal.MigrationsModel 不同。1 sources, IEnumerable1 个目标,DiffContext diffContext,Func 4 diff, Func3 添加,Func 3 remove, Func4[] 谓词)+MoveNext() 在 System.Linq.Enumerable.ConcatIterator 1.MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 源,IEnumerable 1 targets, DiffContext diffContext, Func4 差异,Func 3 add, Func3 删除,Func 4[] predicates)+MoveNext() at System.Linq.Enumerable.ConcatIterator1.MoveNext() 在 Microsoft.EntityFrameworkCore。 Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable 1 operations, DiffContext diffContext) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target) at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) 找不到属性“Alpha.Models.Identity.User”到关系类型的映射。 LockoutEnd',CLR 类型为'Nullable'。PM>

无法为属性找到到关系类型的映射

用户身份模型:

namespace Alpha.Models.Identity
{
    public class User : IdentityUser<int>
    {

    }
}


namespace Microsoft.AspNetCore.Identity
{
    public class IdentityUser<TKey> where TKey : IEquatable<TKey>
    {
       public virtual DateTimeOffset? LockoutEnd { get; set; }
    }
}



namespace Alpha.DataAccess
{
    public class ApplicationDbContext : IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<User>(builder =>
            {
                builder.ToTable("User"); 
            });
        }
    }
}
4

0 回答 0