[英]How to update a table which has a foreign key reference using Entity Framework?
I have the following method: 我有以下方法:
public static Int32 SaveAgent(IAgent i)
{
dc = new mcollectorDataContext(ConnectionString.GetConStr());
//check if the record exists
t_agent matchID = dc.t_agents.Where(x => x.id == i.AgentID).FirstOrDefault();
try
{
if (matchID == null)
{
// if not exists add new row
t_agent _agent = new t_agent
{
wallet = i.Wallet,
branchid = GetBranchID(i.Branch),
lastupdated = i.LastUpdated,
};
dc.t_agents.InsertOnSubmit(_agent);
dc.SubmitChanges();
return _agent.id;
}
else
{
// else update row
matchID.wallet = i.Wallet;
matchID.branchid = GetBranchID(i.Branch);
matchID.lastupdated = i.LastUpdated;
dc.SubmitChanges();
return i.AgentID;
}
}
catch (Exception)
{
throw ;
}
}
This method save new reord but when i try to update, it failed, no record can be updated, but it does not throw also an error. 此方法可以保存新的ordord,但是当我尝试更新时,它失败了,无法更新任何记录,但是它也不会引发错误。 How can fix that problem ?? 如何解决该问题?
maybe try telling entity framework that the model has been modified? 也许尝试告诉实体框架该模型已被修改?
try adding this before calling submitchanges 在致电submitchanges之前尝试添加它
dc.Entry(matchID).State = EntityState.Modified; dc.Entry(matchID).State = EntityState.Modified;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.