[英]Why Entity Framework Core ORM generating wrong query for PostgreSQL?
我正在写一个 web 应用程序。 我的申请信息如下:
在我对其他表的查询中,ORM 效果很好,即使是JSONB格式。 但是,当我对表[WareHouse]进行查询时出现异常
我的 LINQ 查询代码是:
List<WareHouse> wareHouses = db.WareHouses.AsNoTracking()
.Where(x => x.SellerID == dataFuture_Product.SellerID)
.ToList();
Object 与[WareHouse]表的映射为:
public class WareHouse
{
[Key]
public Guid ID { get; set; }
public Guid SellerID { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string GoogleMapEmbedUrl { get; set; }
public string PhoneNumber { get; set; }
public string Fax { get; set; }
public string Facebook { get; set; }
public string Instagram { get; set; }
public string Zalo { get; set; }
public string Email { get; set; }
public bool isActive { get; set; }
public DateTime DateCreate { get; set; }
public DateTime DateModified { get; set; }
public bool isDelete { get; set; }
[Column("Localization", TypeName = "jsonb")]
public Localization Localization { get; set; }
public string SpecificAddress { get; set; }
[NotMapped]
public WareHouse_Product WareHouse_Product { get; set; }
}
Object 与[Localization]表的映射为:
public class Localization
{
[Key]
public Guid ID { get; set; }
public string Code { get; set; }
public Guid? ParentID { get; set; }
public int Level { get; set; }
public DateTime DateCreate { get; set; }
public DateTime DateModify { get; set; }
public bool isDelete { get; set; }
[NotMapped]
public Localization localization { get; set; }
[NotMapped]
public List<LocalizationTranslation> LocalizationTranslations { get; set; }
}
Object 与[LocalizationTranslation]表的映射为:
public class LocalizationTranslation
{
[Key]
public Guid ID { get; set; }
public Guid LocalizationID { get; set; }
public Guid LanguageID { get; set; }
public string Name { get; set; }
public string Title { get; set; }
}
当我执行上面的命令行时,我得到以下异常:
Npgsql.PostgresException (0x80004005): 42703: column w.LocalizationID does not exist
POSITION: 132
at Npgsql.Internal.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|213_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__19_0(DbContext _, Enumerator enumerator)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ProductController.cs:line 213
Exception data:
Severity: ERROR
SqlState: 42703
MessageText: column w.LocalizationID does not exist
Hint: Perhaps you meant to reference the column "w.Localization".
Position: 132
File: d:\pginstaller_13.auto\postgres.windows-x64\src\backend\parser\parse_relation.c
Line: 3513
Routine: errorMissingColumn
上述异常表明我在表中没有[LocalizationID]列。 但是,我的 object 映射目前没有此列的描述。
我尝试或测试过的方法:
@__8__locals2_dataFuture_Product_SellerID_0='e816c2e4-98ad-42ee-9b4a-aa37217aa564'
SELECT w."ID", w."Code", w."DateCreate", w."DateModified", w."Email", w."Facebook", w."Fax", w."GoogleMapEmbedUrl", w."Instagram", w."LocalizationID", w."Name", w."PhoneNumber", w."SellerID", w."SpecificAddress", w."Zalo", w."isActive", w."isDelete"
FROM "WareHouse" AS w
WHERE w."SellerID" = @__8__locals2_dataFuture_Product_SellerID_0
它生成错误?
[Column("Localization", TypeName = "jsonb")]
public Localization Localization { get; set; }
我有一个表[Localization] ,其中的行可以是另一行的父级。
我的问题发生在哪里? 如何解决?
请帮我。
谢谢!
看来object根据properties的属性映射转换器有问题。(Entity Framework Core or Npgsql Entity Framework Core Provider)我使用Fluent ZDB974238714CA8DE634A7CE1D08而不是object映射中定义为JSONB类型有效。 很难理解。 :(
modelBuilder.Entity<WareHouse>()
.ToTable("WareHouse");
modelBuilder.Entity<WareHouse>()
.Property(x => x.Localization)
.HasColumnName("Localization")
.HasColumnType("jsonb");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.