Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 3 add-migration failure on mapping to a relational type

At first I wanted to use EF Core code-first to generate my model in a PostgreSQL database. This failed because I got an exception:

No mapping to a relational type can be found for property 'Webservice.Models.Db.Order.High' with the CLR type 'bool'

So I changed my model and removed the bool but I still get the same exception. I could not find a solution for this problem.

This is my old model class:

public class Order : IEquatable<Order>, ICloneable
{
        public long Id { get; set; }
        public long? DeviceId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public Device Device { get; set; }

        public long? OriginOrderId { get; set; }

        [Required]
        [DataType(DataType.Date)]
        public DateTime RoutineStart { get; set; }

        [Required]
        [EnumDataType(typeof(Routine))]
        public Routine Routine { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public int Pin { get; set; }

        [Required]
        public bool High { get; set; }


        [Required]
        [DataType(DataType.Text)]
        public int TimeInMilliseconds { get; set; }
        public string Description { get; set; }

        [NotMapped]
        public bool Ready { get; set; }

        public OrderState State { get; set; } = OrderState.Idle;
}

I started with add-migration init which resulted in this Exception:

Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.0.0 initialized 'ApplicationDbContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None System.InvalidOperationException: No mapping to a relational type can be found for property 'Webservice.Models.Db.Order.High' with the CLR type 'bool'.

at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty property)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IProperty target, DiffContext diffContext, Boolean inline)+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](IEnumerable
1 sources, IEnumerable1 targets, DiffContext diffContext, Func4 diff, Func3 add, Func3 remove, Func4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator
1.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_0
1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
No mapping to a relational type can be found for property 'Webservice.Models.Db.Order.High' with the CLR type 'bool'.

Then I updated my Model:

public enum PinState
{
    Low,
    High
}

public class Order : IEquatable<Order>, ICloneable
{
        public long Id { get; set; }
        public long? DeviceId { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public Device Device { get; set; }

        public long? OriginOrderId { get; set; }

        [Required]
        [DataType(DataType.Date)]
        public DateTime RoutineStart { get; set; }

        [Required]
        [EnumDataType(typeof(Routine))]
        public Routine Routine { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public int Pin { get; set; }

        [Required]
        public PinState PinState { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public int TimeInMilliseconds { get; set; }
        public string Description { get; set; }

        [NotMapped]
        public bool Ready { get; set; }

        public OrderState State { get; set; } = OrderState.Idle;
}

Then I tried add-migration inittest which resulted in exact the same exception.

My context runs as a scoped service:

services.AddDbContext<ApplicationDbContext>(options =>
                  options.UseNpgsql(Configuration.GetConnectionString("postgres")), ServiceLifetime.Scoped);

On the database side migration history is empty.

I would really appreciate if someone could explain me why this exception occurs.

like image 677
Michael Santos Avatar asked Dec 07 '25 05:12

Michael Santos


1 Answers

I had this problem, when at first I had migrations which had been done for Sql Server database, and then I tried to add new migration for PostgreSQL

So, solution for me was to completely delete all migrations and recreate them all for PostgreSQL.

like image 104
osynavets Avatar answered Dec 08 '25 18:12

osynavets