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

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

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

问题

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

我有简单的一对多关系。

public partial class LinkedDocument
{
    public Guid Id { get; set; }
    public Guid DocumentId { get; set; }

    public virtual TblDocumentData Document { get; set; }
}

public partial class TblDocumentData : IEntity<Guid>
{
    public Guid Id { get; set; }
    // 其他属性
    public virtual ICollection<LinkedDocument> LinkedDocuments { get; set; } = new HashSet<LinkedDocument>();
}

modelBuilder.Entity<LinkedDocument>(
        entity =>
        {
            entity.ToTable("LinkedDocument");

            entity.HasKey(e => new {e.Id, e.DocumentId})
                  .HasName("PK_LinkedDocument")
                  .IsClustered(false);

            entity.Property(e => e.Id).ValueGeneratedNever();

            entity.HasOne(d => d.Document)
                  .WithMany()
                  .HasForeignKey(d => d.DocumentId)
                  .HasConstraintName("FK_LinkedDocument_TBL_DocumentData_DocumentId")
                  .IsRequired()
                  .OnDelete(DeleteBehavior.Cascade);
        });

对于 TBL_DocumentData  Fluent API(仅部分内容)

entity.HasMany(d => d.LinkedDocuments)
                      .WithOne(d => d.Document)
                      .HasForeignKey(d => d.DocumentId)
                      .IsRequired();

在保存时,我有如下代码:

this._ctx.Set<TblDocumentData>().Add(documentData);
this._ctx.Set<LinkedDocument>().Add(new LinkedDocument {Id = attachmentModel.EntityId, DocumentId = documentData.Id});
await this._ctx.SaveAsync(cancellationToken);
但在保存时,我看到错误:列名无效 'TblDocumentDataId'。
从调试控制台中,我可以看到生成的 SQL 脚本如下:

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

根据解决方案搜索 - 无法找到列 `TblDocumentDataId` - 所以这是由 EF 添加的内容。
我在一对多关系的配置中漏掉了什么?

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

英文:

I have simple one-to-many relations.

public partial class LinkedDocument
{
    public Guid Id { get; set; }
    public Guid DocumentId { get; set; }

    public virtual TblDocumentData Document { get; set; }
}

public partial class TblDocumentData : IEntity<Guid>
{
    public Guid Id { get; set; }
    // other props
    public virtual ICollection<LinkedDocument> LinkedDocuments { get; set; } = new HashSet<LinkedDocument>();
}

modelBuilder.Entity<LinkedDocument>(
        entity =>
        {
            entity.ToTable("LinkedDocument");

            entity.HasKey(e => new {e.Id, e.DocumentId})
                  .HasName("PK_LinkedDocument")
                  .IsClustered(false);

            entity.Property(e => e.Id).ValueGeneratedNever();

            entity.HasOne(d => d.Document)
                  .WithMany()
                  .HasForeignKey(d => d.DocumentId)
                  .HasConstraintName("FK_LinkedDocument_TBL_DocumentData_DocumentId")
                  .IsRequired()
                  .OnDelete(DeleteBehavior.Cascade);
        });

and for TBL_DocumentData fluent api (only part)

entity.HasMany(d => d.LinkedDocuments)
                      .WithOne(d => d.Document)
                      .HasForeignKey(d => d.DocumentId)
                      .IsRequired();

On Save I have code like this:

this._ctx.Set<TblDocumentData>().Add(documentData);
this._ctx.Set<LinkedDocument>().Add(new LinkedDocument {Id = attachmentModel.EntityId, DocumentId = documentData.Id});
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:

INSERT INTO [LinkedDocument] ([DocumentId], [Id], [TblDocumentDataId])
      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() 更改为:

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

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

英文:

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

entity
    .HasOne(d => d.Document)
    .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:

确定