简体   繁体   English

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

[英]EF Core 3 add-migration failure on mapping to a relational type

At first I wanted to use EF Core code-first to generate my model in a PostgreSQL database.起初我想使用 EF Core code-first 在 PostgreSQL 数据库中生成我的模型。 This failed because I got an exception:这失败了,因为我有一个例外:

No mapping to a relational type can be found for property 'Webservice.Models.Db.Order.High' with the CLR type 'bool'对于 CLR 类型为“bool”的属性“Webservice.Models.Db.Order.High”,找不到到关系类型的映射

So I changed my model and removed the bool but I still get the same exception.所以我改变了我的模型并删除了bool但我仍然遇到相同的异常。 I could not find a solution for this problem.我找不到这个问题的解决方案。

This is my old model class:这是我的旧模型类:

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

I started with add-migration init which resulted in this Exception:我从add-migration init开始,这导致了这个异常:

Microsoft.EntityFrameworkCore.Infrastructure[10403] 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'. Entity Framework Core 3.0.0 使用提供程序“Npgsql.EntityFrameworkCore.PostgreSQL”初始化“ApplicationDbContext”,选项为:无 System.InvalidOperationException:无法找到属性“Webservice.Models.Db.Order.High”与关系类型的映射CLR 类型 'bool'。

at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty property)在 Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty property)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IProperty target, DiffContext diffContext, Boolean inline)+MoveNext()在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IProperty target, DiffContext diffContext, Boolean inline)+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()在 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable 1 sources, IEnumerable 1 个目标,DiffContext diffContext,Func 4 diff, Func 3 添加,Func 3 remove, Func 4[] 谓词)+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
在 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.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.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.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable
1 个源,IEnumerable 1 targets, DiffContext diffContext, Func 4 diff,Func 3 add, Func 3 移除,Func 4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator
4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator
4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator
1.MoveNext()
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
在 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 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 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.b__0()
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.b__0()

at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)在 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'.对于 CLR 类型为“bool”的属性“Webservice.Models.Db.Order.High”,找不到到关系类型的映射。

Then I updated my Model:然后我更新了我的模型:

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

Then I tried add-migration inittest which resulted in exact the same exception.然后我尝试了add-migration inittest这导致了完全相同的异常。

My context runs as a scoped service:我的上下文作为范围服务运行:

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

On the database side migration history is empty.在数据库端迁移历史是空的。

I would really appreciate if someone could explain me why this exception occurs.如果有人能向我解释为什么会发生此异常,我将不胜感激。

I had this problem, when at first I had migrations which had been done for Sql Server database, and then I tried to add new migration for PostgreSQL我遇到了这个问题,起初我对 Sql Server 数据库进行了迁移,然后我尝试为 PostgreSQL 添加新的迁移

So, solution for me was to completely delete all migrations and recreate them all for PostgreSQL.因此,我的解决方案是完全删除所有迁移并为 PostgreSQL 重新创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM