起初,我想先使用EF Core代码在PostgreSQL数据库中生成模型。失败是因为我遇到异常:
找不到CLR类型为'bool'的属性'Webservice.Models.Db.Order.High'到关系类型的映射
因此,我更改了模型并删除了bool
,但仍然遇到相同的异常。我找不到此问题的解决方案。
这是我的旧型号课:
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]
实体框架核心3.0.0使用提供程序'Npgsql.EntityFrameworkCore.PostgreSQL'初始化了'ApplicationDbContext',选项为:无 System.InvalidOperationException:找不到CLR类型为“ bool”的属性“ Webservice.Models.Db.Order.High”到关系类型的映射。在Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource,IPProperty属性)
在Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IProperty目标,DiffContext diffContext,布尔内联)+ MoveNext()
Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection [T](IEnumerable1 sources, IEnumerable
1个目标,DiffContext diffContext,Func4 diff, Func
3加,Func3 remove, Func
4 []谓词)+ MoveNext ()
在System.Linq.Enumerable.ConcatIterator1.MoveNext()
1处,IEnumerable
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable1 targets, DiffContext diffContext, Func
4 diff,Func3 add, Func
3移除,Func4[] predicates)+MoveNext()
1.MoveNext()
at System.Linq.Enumerable.ConcatIterator
在Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable1 operations, DiffContext diffContext)
1.b__0()
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.Design.OperationExecutor.OperationBase.Execute(动作)上
对于CLR类型为“ bool”的属性“ Webservice.Models.Db.Order.High”,找不到与关系类型的映射。
然后我更新了模型:
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);
在数据库端,迁移历史记录为空。
如果有人能向我解释此异常的原因,我将不胜感激。
答案 0 :(得分:2)
我遇到了这个问题,起初我有针对Sql Server数据库进行的迁移,然后尝试为PostgreSQL添加新的迁移
所以,对我来说,解决方案是完全删除所有迁移,然后为PostgreSQL重新创建所有迁移。