我将 Asp.net core 3 C# 与 Entity Framework core 3 一起使用,并且我正在尝试使用 PostgreSQL 服务器。
这是我的模型,包含在异常中命名的 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;
}
上下文是一个作用域服务,应该无关紧要,但是......
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,我将非常感激。
最佳答案
PostgreSQL 实际上托管了一个 _EFMigrations 表。错误从那里开始传播。我记得当我尝试使用 .NET Core 和 EFCore 从 SQLServer 迁移到 PostgreSQL 时遇到过一个这样的问题。 如果你想这样做
使用此命令可让您逐一回滚所有迁移。
EntityframeworkCore\Update-Database 0
通过运行进行新的迁移
EntityFrameworkCore\Add-Migration Init
PostGreSQL 现在应该能够跟踪所有迁移和数据库状态。
关于c# - PostgreSQL EF.Core DB-First 无效的数据库模型 : No mapping to a relational type can be found for property 'Db.Order.High' with the CLR type 'bool' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58984353/