c# - 映射到关系类型时 EF Core 3 添加迁移失败

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

起初我想使用 EF Core 代码优先在 PostgreSQL 数据库中生成我的模型。这失败了,因为我有一个异常(exception):

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

所以我更改了模型并删除了 bool但我仍然遇到同样的异常(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;
}

我从 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.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()<br/> at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext()<br/> at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 sources, IEnumerable1 targets, DiffContext diffContext, Func4 diff, Func3 add, Func3 remove, Func4[] predicates)+MoveNext()<br/> at System.Linq.Enumerable.ConcatIterator1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable1 operations, DiffContext diffContext)<br/> at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)<br/> at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)<br/> at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)<br/> at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)<br/> at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()<br/> at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.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'.

然后我更新了我的模型:

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

然后我试了add-migration inittest这导致了完全相同的异常。

我的上下文作为范围服务运行:

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

在数据库端迁移历史是空的。

如果有人能向我解释为什么会出现此异常,我将不胜感激。

最佳答案

我遇到了这个问题,起初我对 Sql Server 数据库进行了迁移,然后我尝试为 PostgreSQL 添加新的迁移

因此,对我来说,解决方案是完全删除所有迁移并为 PostgreSQL 重新创建它们。

关于c# - 映射到关系类型时 EF Core 3 添加迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59006472/

相关文章:

c# - 对象 xml 反序列化问题?

c# - 客户端评估 efcore 后无法处理集合操作

c# - 加载 native DLL 的引用中的 DllImport

c# - 如何使我的 NFC 应用程序在 Windows Phone 中成为 "trusted app"?

c# - 一个 list 中有更多不同的项目

json - 为什么 postman 中显示 "The field is required",即使它可以为空?

c# - EF Core 不会在 SaveChanges()/SaveChangesAsync() 上引发异常

postgresql - TypeORM 通过 json 属性查询具有 jsonb 列的实体

postgresql - 更新查询 Postgresql 更新缓慢

PostgreSQL 主服务器在复制流上挂起