1

I want to create a migration for creating a new table in my database but after executing the Add-Migration MsSql_Add_ArticleCategory_Table, I got this error:

No mapping to a relational type can be found for property 'Alpha.Models.Identity.User.LockoutEnd' with the CLR type 'Nullable'.

How can I solve it (I've used asp.net core 3.1 iin my project)?

PM> Add-Migration MsSql_Add_ArticleCategory_Table Build started... Build succeeded. Microsoft.EntityFrameworkCore.Model.Validation[30000] No type was specified for the decimal column 'RateCounter' on entity type 'Article'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values using 'HasColumnType()'. Microsoft.EntityFrameworkCore.Model.Validation[30000] No type was specified for the decimal column 'Rate' on entity type 'Rating'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values using 'HasColumnType()'. Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 3.1.3 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=Alpha.DataAccess System.InvalidOperationException: No mapping to a relational type can be found for property 'Alpha.Models.Identity.User.LockoutEnd' with the CLR type 'Nullable'. at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty property) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IProperty source, IProperty target, DiffContext diffContext)+MoveNext() at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 sources, IEnumerable1 targets, DiffContext diffContext, Func4 diff, Func3 add, Func3 remove, Func4[] predicates)+MoveNext() at 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](IEnumerable1 sources, IEnumerable1 targets, DiffContext diffContext, Func4 diff, Func3 add, Func3 remove, Func4[] predicates)+MoveNext() at System.Linq.Enumerable.ConcatIterator1.MoveNext() at 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_01.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) No mapping to a relational type can be found for property 'Alpha.Models.Identity.User.LockoutEnd' with the CLR type 'Nullable'. PM>

No mapping to a relational type can be found for property

User Identity Model:

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"); 
            });
        }
    }
}
x19
  • 8,277
  • 15
  • 68
  • 126
  • 1
    Can you add your model class to see which fields are causing trouble? – Métoule May 27 '20 at 06:04
  • I added the User Model. – x19 May 27 '20 at 11:25
  • That's strange, I had no issue with a field of type `System.DateTimeOffset?`. Can you check that your `DateTimeOffset` is in fact coming from the `System` namespace? It might be that one of your using statement is pulling in a different one that can't be mapped by default. – Métoule May 27 '20 at 12:36

0 Answers0