更新外键时出现Linq to SQL错误

本文关键字:Linq to SQL 错误 更新 | 更新日期: 2023-09-27 18:22:08

我是一个试图更新外键的初学者,但它显示了这个错误

无法将类型"DynaDNS.DAO.GroupDetail"隐式转换为'long'

主机DNSDetailsGroupId-用于Groupdetails的FK主机DNSId

组详细信息GroupId-PK

public bool UpdateHostDns(HostDNSDetail HostObj)
{ 
DynaDBDataContext ctx = GetDataContext();
var ExistingDns = (from HostDNS in ctx.HostDNSDetails
             where (HostDNS.HostDNSId) == (HostObj.HostDNSId)
             select HostDNS).FirstOrDefault();
   if (ExistingDns != null)
    {
      ExistingDns.GroupId = ctx.GroupDetails.Single(Existing =>Existing.GroupId ==    HostObj.GroupId); 
    // i m trying to update from the value from "HostObj.GroupId".
  }
}

更新外键时出现Linq to SQL错误

您正试图用GroupDetails.Single选择整个对象。所以,不是:

ExistingDns.GroupId = ctx.GroupDetails.Single(Existing =>Existing.GroupId ==    HostObj.GroupId);

你需要做一些类似的事情:

long ExistingDnsId = (from c in ctx.GroupDetails
                      where c.GroupId == HostObj.GroupId
                      select c.GroupId).SingleOrDefault();