PostgreSQL EF.Core DB-First Invalid DB Model: No mapping to a relational type can be found for property 'Db.Order.High' with the CLR type 'bool'

c# ef-core-3.0 entity-framework-core postgresql

Question

I'm using Asp.net core 3 C# with Entity Framework core 3 and I'm trying to use a PostgreSQL server.

This is my Model containing the bool High which is named in the Exception:

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

The Context is a Scoped Service which should not matter, but...

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

I tried to add a initial Migration with Add-Migration init and this is the whole Exception:

add-migration init 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.Diff(IProperty source, IProperty target, DiffContext diffContext)+MoveNext() 
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext() 
   at 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](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext() 
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext() 
   at 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_0`1.<Execute>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'.

A workaround for this would be to Inherit "ViewOrder" from Order while only Order is stored in the DB:

Order

public int high { get; set; }

ViewOrder : Order

public bool High
{
    get => high == 1;
    set => high = value ? 1 : 0;
}

But i would rather not do this. There must be something like bit in T-SQL. I would really aprecciate if someone could explain me how to use bool in a PostgreSQL Model.

1
2
11/21/2019 10:59:10 PM

Accepted Answer

The solution to this problem was to delete all old migrations. It showed this error about "bool" but the error had nothing to do with the errormessage. I dont get it, but deleting all migrations and initializing the database new solved it.

0
11/30/2019 9:32:59 PM

Popular Answer

PostgreSQL actually hosts a _EFMigrations Table. The error starts propagating from there. I remember facing one such issue when i was trying to migrate from SQLServer to PostgreSQL with .NET Core and EFCore. If you were trying to do that

use this command which will let you rollback all migrations one by one.

EntityframeworkCore\Update-Database 0

Make a new Migration by running

EntityFrameworkCore\Add-Migration Init

PostGreSQL should now be able to keep track of all the migrations and the Database State.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow