【发布时间】:2020-01-05 01:23:29
【问题描述】:
我遇到这个问题已经有一段时间了,老实说,我自己也对此感到困惑,所以如果我没有成功地解释它,请原谅我。
我正在尝试将一些数据插入一个名为CommunicationAttachment 的表中,该表与Communication 关联为一对多关系;每个通信都可以有很多附件。
事情是我得到了:
UpdateException:列名无效:“Communication_CommunicationId
当我尝试插入附件列表时。
请注意,我使用的是存储库模式,但我什至尝试了正常方式,但问题没有解决。
我尝试跟踪数据库上发生的事务,发现它使用Insert 语句发送Communication_CommunicationId,但没有这样的列。我很确定我没有发送这样的专栏。
这是我的代码(添加新的Communication 时会发生这种情况);首先我打电话给CasefileAttachments从他们那里复制,Communications与CaseFiles相关:
public List<CorrespondenceAttachment> GetCaseFileAttachments(List<Guid> CorrespondenceAttachmentIds)
{
List<CorrespondenceAttachment> originalAttachments = new List<CorrespondenceAttachment>();
foreach (var item in CorrespondenceAttachmentIds)
{
var attachment = QueryData.Query<CorrespondenceAttachment>().Where(att => att.CorrespondenceAttachmentID == item).FirstOrDefault();
originalAttachments.Add(attachment);
}
return originalAttachments;
}
然后我复制CaseFileAttachments 并创建CommunicationAttachments 的新对象:
public List<CommunicationAttachment> CopyCaseFileAttachmentsToCommunication(List<CorrespondenceAttachment> originalAttachments,Guid communicationId)
{
var communicationAttachments = new List<CommunicationAttachment>();
if (originalAttachments.Any())
{
foreach (var attachmentRef in originalAttachments)
{
var CommunicationAttachmentId = Guid.NewGuid();
communicationAttachments.Add(new CommunicationAttachment()
{
CommunicationAttachmentId = CommunicationAttachmentId,
DmsFileId = CommunicationAttachmentId,
CommunicationId = communicationId,
AttachmentTitle = attachmentRef.AttachmentTitle,
MimeType = attachmentRef.MimeType,
NewVersionID = null,
UploadDate = DateTime.Now,
Size = attachmentRef.Size,
Version = "0001",
AttachmentsGroupId = attachmentRef.AttachmentsGroupId,
DocumentId = attachmentRef.DocumentId,
RelativePath = attachmentRef.RelativePath,
Extension = attachmentRef.Extension,
AttachmentSubject = attachmentRef?.AttachmentSubject,
ExternalContactID = attachmentRef?.ExternalContactID,
AttachmentNumber = string.IsNullOrEmpty(attachmentRef?.AttachmentNumber) ? null : attachmentRef.AttachmentNumber,
TemplatedmsId = attachmentRef.TemplatedmsId,
State = eSense.Framework.Data.ObjectState.Added,
});
}
}
return communicationAttachments;
}
上面的方法是这样调用的:
public void AddNewCommunication(CommunicationDto communicationDto)
{
var communication = communicationDto
if (communicationDto.CommunicationAttachmentIdList.Any())
{
caseFileAttachments = GetCaseFileAttachments(communicationDto.CommunicationAttachmentIdList);
if (caseFileAttachments.Any())
{
commAttachments = CopyCaseFileAttachmentsToCommunication(caseFileAttachments, communication.CommunicationId);
}
}
communication.Attachments = commAttachments;
Save(communication)
}
那么我得到错误的列名可能是什么问题?
这是Communication和CommunicationAttachment之间的关系
注意我只添加了重要字段,所以如果声明与实体不匹配,请不要打扰
通信实体:
public class Communication : BaseEntity
{
public Communication()
{
Attachments = new HashSet<CommunicationAttachment>();
}
[Key]
public Guid CommunicationId { get; set; }
public string Subject { get; set; }
public string CommunicationNumber { get; set; }
public virtual ICollection<CommunicationAttachment> Attachments { get; set; }
public DateTime DateCreated { get; set; }
public Guid? PreviousCommunicationId { get; set; }
[ForeignKey("PreviousCommunicationId")]
public virtual Communication PreviousCommunication { get; set; }
}
CommunicationAttachment 实体:
public class CommunicationAttachment : AttachmentBaseWithDelegation<Guid>
{
public override Guid PrimaryId
{
get
{
return this.CommunicationAttachmentId;
}
}
public CommunicationAttachment()
{
}
[Key]
public Guid CommunicationAttachmentId { get; set; }
private string _attachmentNumber;
public string AttachmentNumber { get; set; }
[ForeignKey("NewVersionID")]
public virtual CommunicationAttachment CaseFileAttachmentNewerVersion { get; set; }
public Guid CommunicationId { get; set; }
[ForeignKey("CommunicationId")]
public virtual Communication Communication { get; set; }
}
对不起,如果您发现我的问题很难理解,我自己也很困惑!
提前致谢。
【问题讨论】:
-
1. SQL Profiler 上显示了什么? 2.如何在本地进行模型更改?迁移是否成功应用于本地开发数据库?错误的列名表明模型和模式不同步,即,甚至将 ORM 带入等式的全部意义。否则为什么还要使用 ORM。
-
在 Profiler 中传递 CommunicationId 和 Communication_CommunicationId。是的,迁移已成功应用。我使用 ORM 与公司的同事相处。
标签: c# sql asp.net entity-framework