英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论