我是初学者,尝试更新外键但显示此错误
无法将类型“
DynaDNS.DAO.GroupDetail
”隐式转换为 'long
'
HostDNSDetail GroupId-FK for Groupdetails HostDNSId
GroupDetails 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".
}
}
答案 0 :(得分:3)
您正尝试使用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();