一对多:在从某处插入时出现了在 EF Core 中未知的列

huangapple go评论117阅读模式
英文:

One-to-many: on insert from somewhere appears unknown column with EF core

问题

以下是您提供的代码的中文翻译部分:

  1. 我有简单的一对多关系。
  2. public partial class LinkedDocument
  3. {
  4. public Guid Id { get; set; }
  5. public Guid DocumentId { get; set; }
  6. public virtual TblDocumentData Document { get; set; }
  7. }
  8. public partial class TblDocumentData : IEntity<Guid>
  9. {
  10. public Guid Id { get; set; }
  11. // 其他属性
  12. public virtual ICollection<LinkedDocument> LinkedDocuments { get; set; } = new HashSet<LinkedDocument>();
  13. }
  14. modelBuilder.Entity<LinkedDocument>(
  15. entity =>
  16. {
  17. entity.ToTable("LinkedDocument");
  18. entity.HasKey(e => new {e.Id, e.DocumentId})
  19. .HasName("PK_LinkedDocument")
  20. .IsClustered(false);
  21. entity.Property(e => e.Id).ValueGeneratedNever();
  22. entity.HasOne(d => d.Document)
  23. .WithMany()
  24. .HasForeignKey(d => d.DocumentId)
  25. .HasConstraintName("FK_LinkedDocument_TBL_DocumentData_DocumentId")
  26. .IsRequired()
  27. .OnDelete(DeleteBehavior.Cascade);
  28. });
  29. 对于 TBL_DocumentData Fluent API(仅部分内容)
  30. entity.HasMany(d => d.LinkedDocuments)
  31. .WithOne(d => d.Document)
  32. .HasForeignKey(d => d.DocumentId)
  33. .IsRequired();
  34. 在保存时,我有如下代码:
  35. this._ctx.Set<TblDocumentData>().Add(documentData);
  36. this._ctx.Set<LinkedDocument>().Add(new LinkedDocument {Id = attachmentModel.EntityId, DocumentId = documentData.Id});
  37. await this._ctx.SaveAsync(cancellationToken);
  38. 但在保存时,我看到错误:列名无效 'TblDocumentDataId'
  39. 从调试控制台中,我可以看到生成的 SQL 脚本如下:
  40. INSERT INTO [LinkedDocument] ([DocumentId], [Id], [TblDocumentDataId])
  41. VALUES (@p22, @p23, @p24);
  42. 根据解决方案搜索 - 无法找到列 `TblDocumentDataId` - 所以这是由 EF 添加的内容。
  43. 我在一对多关系的配置中漏掉了什么?

如果您需要更多帮助或有其他问题,请随时提问。

英文:

I have simple one-to-many relations.

  1. public partial class LinkedDocument
  2. {
  3. public Guid Id { get; set; }
  4. public Guid DocumentId { get; set; }
  5. public virtual TblDocumentData Document { get; set; }
  6. }
  7. public partial class TblDocumentData : IEntity<Guid>
  8. {
  9. public Guid Id { get; set; }
  10. // other props
  11. public virtual ICollection<LinkedDocument> LinkedDocuments { get; set; } = new HashSet<LinkedDocument>();
  12. }
  13. modelBuilder.Entity<LinkedDocument>(
  14. entity =>
  15. {
  16. entity.ToTable("LinkedDocument");
  17. entity.HasKey(e => new {e.Id, e.DocumentId})
  18. .HasName("PK_LinkedDocument")
  19. .IsClustered(false);
  20. entity.Property(e => e.Id).ValueGeneratedNever();
  21. entity.HasOne(d => d.Document)
  22. .WithMany()
  23. .HasForeignKey(d => d.DocumentId)
  24. .HasConstraintName("FK_LinkedDocument_TBL_DocumentData_DocumentId")
  25. .IsRequired()
  26. .OnDelete(DeleteBehavior.Cascade);
  27. });

and for TBL_DocumentData fluent api (only part)

  1. entity.HasMany(d => d.LinkedDocuments)
  2. .WithOne(d => d.Document)
  3. .HasForeignKey(d => d.DocumentId)
  4. .IsRequired();

On Save I have code like this:

  1. this._ctx.Set<TblDocumentData>().Add(documentData);
  2. this._ctx.Set<LinkedDocument>().Add(new LinkedDocument {Id = attachmentModel.EntityId, DocumentId = documentData.Id});
  3. await this._ctx.SaveAsync(cancellationToken);

but on save I see error: Invalid column name 'TblDocumentDataId'.
From debug console I can see that SQL script is generated like this:

  1. INSERT INTO [LinkedDocument] ([DocumentId], [Id], [TblDocumentDataId])
  2. VALUES (@p22, @p23, @p24);

Searching by solution - cannot find column TblDocumentDataId - so this is something added by EF.

What I missed in configuration of my one-to-many relation?

答案1

得分: 2

尝试将 entity.HasOne(d => d.Document).WithMany() 更改为:

  1. entity
  2. .HasOne(d => d.Document)
  3. .WithMany(data => data.LinkedDocuments)

您可以删除 entity.HasMany(d => d.LinkedDocuments) 中的设置(我认为设置两次没有意义)。

英文:

Try changing entity.HasOne(d => d.Document).WithMany() to:

  1. entity
  2. .HasOne(d => d.Document)
  3. .WithMany(data => data.LinkedDocuments)

And you can remove the setup from entity.HasMany(d => d.LinkedDocuments) (I would argue that there is no point to setup the same twice).

huangapple
  • 本文由 发表于 2023年7月13日 17:32:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定