PostgreSQL EF.Core DB-First无效的数据库模型:找不到CLR类型为'bool'的属性'Db.Order.High'的关系类型映射

时间:2019-11-21 21:55:28

标签: c# postgresql entity-framework-core ef-core-3.0

我将Asp.net core 3 C#与Entity Framework core 3一起使用,并且尝试使用PostgreSQL服务器。

这是我的模型,其中包含布尔值,该异常在异常中被命名为

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

上下文是作用域服务,应该无关紧要,但是...

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

我尝试使用 Add-Migration init 添加初始迁移,这是整个异常:

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'.

一种解决方法是从Order继承“ ViewOrder”,而仅将Order存储在数据库中:

订购

public int high { get; set; }

ViewOrder:订单

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

但是我宁愿不这样做。 T-SQL中必须有类似 bit 的内容。 如果有人可以向我解释如何在PostgreSQL模型中使用bool,我将非常感激。

4 个答案:

答案 0 :(得分:4)

PostgreSQL实际上托管一个_EFMigrations表。错误从那里开始传播。我记得当我尝试使用.NET Core和EFCore从SQLServer迁移到PostgreSQL时遇到这样的问题。 如果您尝试这样做

使用此命令,将使您一个个地回滚所有迁移。

EntityframeworkCore\Update-Database 0

通过运行

进行新的迁移
EntityFrameworkCore\Add-Migration Init

PostGreSQL现在应该能够跟踪所有迁移和数据库状态。

答案 1 :(得分:3)

@Michael Santos建议 问题: 对我来说,实际上首先我使用Visual Studio的默认身份验证创建了该项目,该项目为sql server数据库创建了迁移,而我需要将我的Web应用程序连接到Postgresql。因此,当我运行Update-Database时,我向我显示“空引用”的错误,而当我添加新的迁移时,由于已经添加了默认迁移,因此向我显示了“无映射”的错误 解决方案: 删除旧的迁移和ApplicationDbContextModelSnapshot 创建新的迁移(添加迁移migrationName) 更新数据库

答案 2 :(得分:1)

https://www.npgsql.org/doc/types/basic.html

看到这个, PostgreSQL类型需要使用[boolean]为默认.NET类型[bool]

答案 3 :(得分:0)

此问题的解决方案是删除所有旧迁移。它显示了有关“布尔”的错误,但该错误与错误消息无关。 我不明白,但是删除所有迁移并重新初始化数据库就解决了。