为什么EF核心无效的实体原因是“预期类型是system.String'但是实际价值是无效的?”

发布于 2025-01-23 23:34:30 字数 1241 浏览 2 评论 0原文

之间有多一关系

modelBuilder.Entity<Party>()
  .HasMany(p => p.PartyMembers)
  .WithOne(p => p.Party);

使用实体框架6和.NET 6,我在partystatentity 有一个party

[NotLogged] [JsonIgnore] public Party? Party { get; set; }

的确,我们可以看到party根据自动生成的迁移而配置为无效:

migrationBuilder.AlterColumn<string>(
                name: "PartyId",
                table: "StatEntities",
                type: "varchar(36)",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "varchar(36)")
                .Annotation("MySql:CharSet", "latin1")
                .OldAnnotation("MySql:CharSet", "latin1");
  • 根据__ __ efmigrations history,迁移是成功应用。
  • 数据库中存在null party>的值。

但是,当我尝试查询statentity具有null的值party> party 时,我会收到以下内容:

在读取属性'statentity.partyid'的数据库值时发生错误。预期的类型为“系统。弦”,但实际值为null。

请注意,我是不是尝试.include(statentity =&gt; statentity.party)。似乎EF Core不允许为影子属性提供零值,即使它被称为此类属性。

Using Entity Framework 6 and .NET 6, I have a many-to-one relationship between Party and StatEntity:

modelBuilder.Entity<Party>()
  .HasMany(p => p.PartyMembers)
  .WithOne(p => p.Party);

Where the StatEntity may not necessarily have a Party:

[NotLogged] [JsonIgnore] public Party? Party { get; set; }

Indeed, we can see that PartyId is configured as nullable based upon the auto generated migration:

migrationBuilder.AlterColumn<string>(
                name: "PartyId",
                table: "StatEntities",
                type: "varchar(36)",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "varchar(36)")
                .Annotation("MySql:CharSet", "latin1")
                .OldAnnotation("MySql:CharSet", "latin1");
  • According to __EFMigrationsHistory, the migration was successfully applied.
  • There exist null values for PartyId in the database.

However, when I try to query a StatEntity that has a null value for PartyId, I receive the following:

An error occurred while reading a database value for property 'StatEntity.PartyId'. The expected type was 'System.String' but the actual value was null.

Note that I am not attempting to .Include(statEntity => statEntity.Party). It seems that EF Core is not allowing null values for the shadow property, even though it is declared as such.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2025-01-30 23:34:30

作为一个工作障碍,明确定义statentity上的影子属性似乎可以解决该问题:

    [NotLogged] [JsonIgnore] public string? PartyId { get; set; }

    [NotLogged] [JsonIgnore] public FurballParty? Party { get; set; }

奇怪的是,运行dotnet ef ef迁移添加blah创建一个空迁移,因此肯定数据库方面没有错。它似乎是EFCORE中的一个错误,它导致基础阴影属性无法正确定义为无效。

As a work-around, explicitly defining the shadow property on the StatEntity appears to fix the problem:

    [NotLogged] [JsonIgnore] public string? PartyId { get; set; }

    [NotLogged] [JsonIgnore] public FurballParty? Party { get; set; }

Curiously, running a dotnet ef migrations add blah creates an empty migration, so definitely nothing wrong with the database aspect. It appears to be a bug in EFCore that causes the underlying shadow property to not be correctly defined as nullable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文