1
我试图更新外键的初学者,但它显示了这个错误LINQ到SQL错误的更新外键
无法隐式转换类型“
DynaDNS.DAO.GroupDetail
”到 “long
”
HostDNSDetail GroupId- FK为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".
}
}
感谢乌拉圭回合的答复,但它仍然抛出了同样的错误“System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException:操作无效由于目标的当前状态 “ – KATy 2012-02-02 11:08:19
哦,我看到你在这里试图做什么...如果你想设置ExistingDns.GroupId,然后尝试,而不是:ExistingDns = ctx.GroupDetails.Single(Existing = > Existing.GroupId == HostObj.GroupId);您应该设置关联属性而不是外键。 – TheBoyan 2012-02-02 11:36:35
我发现它,我必须将它设置为GroupDetails的对象,我的意思是,ExistingDns.GroupDetail = ctx.GroupDetails.SingleOrDefault(c => c.GroupId == HostObj.GroupId); //存在于ExistingDns中的GroupDetail [GroupDetail表的对象]。 – KATy 2012-02-02 11:55:20