我有以下方法:
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 ;
}
}
此方法保存新的reord但是当我尝试更新时,它失败了,没有记录可以更新,但它也不会引发错误。 如何解决这个问题??
答案 0 :(得分:0)
也许尝试告诉实体框架该模型已被修改?
尝试在调用submitchanges
之前添加它dc.Entry(matchID).State = EntityState.Modified;