我想创建一个迁移来在我的数据库中创建一个新表,但是在执行之后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, IEnumerable
1 个目标,DiffContext diffContext,Func4 diff, Func
3 添加,Func3 remove, Func
4[] 谓词)+MoveNext() 在 System.Linq.Enumerable.ConcatIterator1.MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable
1 源,IEnumerable1 targets, DiffContext diffContext, Func
4 差异,Func3 add, Func
3 删除,Func4[] predicates)+MoveNext() at System.Linq.Enumerable.ConcatIterator
1.MoveNext() 在 Microsoft.EntityFrameworkCore。 Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable1 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_0
1.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");
});
}
}
}