Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列名错误实体框架中的问题_C#_Sql_Asp.net_Entity Framework - Fatal编程技术网

C# 列名错误实体框架中的问题

C# 列名错误实体框架中的问题,c#,sql,asp.net,entity-framework,C#,Sql,Asp.net,Entity Framework,我已经面临这个问题一段时间了,老实说,我自己也对它感到困惑,所以如果我没有成功地解释它,请原谅 我试图将一些数据插入一个名为CommunicationAttachment的表中,该表与通信关联为一对多关系;每一次交流都可能有很多附件 问题是我得到了: UpdateException:列名无效:“通信\通信ID” 当我尝试插入附件列表时 请注意,我使用的是存储库模式,但我甚至尝试了正常的方法,问题没有得到解决 我尝试跟踪数据库上发生的事务,发现它使用Insert语句发送Communication\

我已经面临这个问题一段时间了,老实说,我自己也对它感到困惑,所以如果我没有成功地解释它,请原谅

我试图将一些数据插入一个名为
CommunicationAttachment
的表中,该表与
通信
关联为一对多关系;每一次交流都可能有很多附件

问题是我得到了:

UpdateException:列名无效:“通信\通信ID”

当我尝试插入附件列表时

请注意,我使用的是存储库模式,但我甚至尝试了正常的方法,问题没有得到解决

我尝试跟踪数据库上发生的事务,发现它使用
Insert
语句发送
Communication\u CommunicationId
,但没有这样的列。我很确定我没有发送这样的列

这是我的代码(在添加新的
通信时会发生这种情况);首先,我调用
CasefileAttachments
从中复制,并且
通信与
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;
}
上述方法被称为类似这样的方法:

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)
}
那么,我得到一个错误的列名会是什么问题呢

以下是
通信
通信附件

注意我只添加了重要字段,所以如果声明与实体不匹配,就不用麻烦了

通信实体:

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; }

}
公共类通信:BaseEntity
{
公共传播()
{
附件=新的HashSet();
}
[关键]
公共Guid通信ID{get;set;}
公共字符串主题{get;set;}
公共字符串通信号{get;set;}
公共虚拟ICollection附件{get;set;}
public DateTime DateCreated{get;set;}
公共Guid?PreviousCommunicationId{get;set;}
[外键(“以前的通讯ID”)]
公共虚拟通信以前的通信{get;set;}
}
通信连接实体:

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; }


}
公共类通信附件:AttachmentBaseWithDelegation
{
公共覆盖Guid PrimaryId
{
得到
{
返回此.CommunicationAttachmentId;
}
}
公共传播附件
{
}
[关键]
公共Guid通信附件ID{get;set;}
私有字符串_attachmentNumber;
公共字符串AttachmentNumber{get;set;}
[ForeignKey(“NewVersionID”)]
公共虚拟通信附件CaseFileAttachmentNewServerVersion{get;set;}
公共Guid通信ID{get;set;}
[外键(“通讯ID”)]
公共虚拟通信{get;set;}
}
对不起,如果你觉得很难理解我的问题,我自己也很困惑


提前感谢。

这通常是实体之间关系设置不正确的情况。如果通信的主键为“通信ID”,则EF应该按照约定解决此关系

我注意到您注释掉了一行,用于在新实体上设置通信ID:

//CommunicationId=CommunicationId,

通讯附件中有哪些字段?是否有通讯ID?是否有通讯导航属性?您正在使用哪些配置设置

例如,使用fluent配置,我会有如下内容:

(通信权限配置)

如果CommunicationAttachment具有返回到Communication的导航属性和名为CommunicationId的FK字段

HasMany(x => x.CommunicationAttachments)
   .WithRequired(x => x.Communication)
   .HasForeignKey(x => x.CommunicationId);
如果附件实体具有导航属性,但实体中没有映射的FK

HasMany(x => x.CommunicationAttachments)
   .WithRequired(x => x.Communication)
   .Map(x => x.MapKey("CommunicationId"));
HasMany(x => x.CommunicationAttachments)
   .WithRequired()
   .HasForeignKey(x => x.CommunicationId);
HasMany(x => x.CommunicationAttachments)
   .WithRequired()
   .Map(x => x.MapKey("CommunicationId"));
如果附件实体没有导航属性,但实体中有FK

HasMany(x => x.CommunicationAttachments)
   .WithRequired(x => x.Communication)
   .Map(x => x.MapKey("CommunicationId"));
HasMany(x => x.CommunicationAttachments)
   .WithRequired()
   .HasForeignKey(x => x.CommunicationId);
HasMany(x => x.CommunicationAttachments)
   .WithRequired()
   .Map(x => x.MapKey("CommunicationId"));
最后,如果附件实体既没有导航属性也没有映射的FK

如果附件实体没有导航属性,但实体中有FK

HasMany(x => x.CommunicationAttachments)
   .WithRequired(x => x.Communication)
   .Map(x => x.MapKey("CommunicationId"));
HasMany(x => x.CommunicationAttachments)
   .WithRequired()
   .HasForeignKey(x => x.CommunicationId);
HasMany(x => x.CommunicationAttachments)
   .WithRequired()
   .Map(x => x.MapKey("CommunicationId"));
我非常喜欢显式映射而非约定,因为很清楚什么映射到什么,以及如何映射,以解决潜在的映射冲突。如果其他类似的关系似乎在工作,而只有这一个在发挥作用,我会在字段名中寻找可能的键入错误。对于上面这样的映射集合,设置a
Communication.CommunicationAttachments.Add(attachment)
应在附件上设置FK/相关实体,而无需手动显式设置FK或相关实体

另一项说明:

从您的示例中,我看到您正在使用Guid.NewGuid()在客户端手动设置主键。通常,最好允许数据库管理PK生成,并让EF管理FK分配,以确保相关实体自动将FK获取到新插入的行。而不是SQL的
NewId()
或使用
Guid.NewGuid()
,建议使用顺序UUID。在SQL Server中,这是
NewSequentialId()
。对于客户端设置,可以通过系统DLL调用获取ID或简单地重新哈希Guid字节来复制顺序UUID模式。请参阅:

GUID仍然具有相同的唯一性,字节只是被简单地安排为更连续和更实用的数据库索引,以减少页面碎片。缺点是ID更可预测。根据您的数据库引擎,您可能希望根据数据库是否已优化,以对页面进行索引来定制算法低阶或高阶字节


为数据库(顺序或其他)使用GUID时,应确保在数据库上有计划的索引维护作业。使用顺序ID,此作业将运行得更快,并使索引表更紧凑。

1.SQL Prof