[英]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'
我正在使用 Asp.net 核心 3 C# 和實體框架核心 3,我正在嘗試使用 PostgreSQL 服務器。
這是我的 Model 包含在異常中命名的 bool 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 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;
}
Context 是一個 Scoped Service,它應該無關緊要,但是......
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 存儲在 DB 中:
命令
public int high { get; set; }
查看訂單:訂單
public bool High
{
get => high == 1;
set => high = value ? 1 : 0;
}
但我寧願不這樣做。 T-SQL 中一定有類似bit的東西。 如果有人能解釋我如何在 PostgreSQL Model 中使用 bool,我會非常感激。
PostgreSQL 實際上托管了一個 _EFMigrations 表。 錯誤從那里開始傳播。 我記得當我嘗試使用 .NET Core 和 EFCore 從 SQLServer 遷移到 PostgreSQL 時遇到了一個這樣的問題。 如果你試圖這樣做
使用此命令,您可以一一回滾所有遷移。
EntityframeworkCore\Update-Database 0
通過運行進行新的遷移
EntityFrameworkCore\Add-Migration Init
PostGreSQL 現在應該能夠跟蹤所有遷移和數據庫 State。
正如@Michael Santos 建議的問題:對我來說,實際上首先我使用Visual Studio 的默認身份驗證創建了該項目,該身份驗證為sql 服務器數據庫創建遷移,而我需要將我的web 應用程序連接到ZC26C231D9E6ACBC37FF10118D26EZ26。 因此,當我運行 Update-Database 時,我向我顯示“空引用”的錯誤,當我添加新遷移時,由於已添加默認遷移,它向我顯示“無映射”錯誤解決方案:刪除舊遷移和 ApplicationDbContextModelSnapshot 創建新遷移(Add-Migration migrationName ) 更新數據庫
https://www.npgsql.org/doc/types/basic.html
要看到這一點,PostgreSQL 類型需要使用 [boolean] 到 Default .NET 類型 [bool]
此問題的解決方案是刪除所有舊遷移。 它顯示了有關“bool”的錯誤,但該錯誤與錯誤消息無關。 我不明白,但刪除所有遷移並初始化數據庫新解決了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.