简体   繁体   English

如何使用实体框架更新具有外键引用的表?

[英]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.

相关问题 使用实体框架中的数据库优先方法,如何仅从具有多个外键关系的单个表中获取数据? - Using Database first approach in Entity framework, how to fetch the data from a single table only which has multiple foreign key relationship? 使用实体框架更新外键 - Update foreign key using Entity Framework 实体框架将现有外键引用更新为另一个 - Entity Framework Update existing foreign Key reference to another 带有外键引用的Entity Framework Core更新列 - Entity Framework Core update column with foreign key reference 如何将第一个表的主键值更新为第二个表的外键(实体框架代码第一个模型)? - How to update primary key values of first table into foreign keys in the second table (Entity framework code first model)? 如何使用Entity Framework中另一个表上的外键引用的汇总值更新表的所有行 - How to update all rows of a table with an aggregate value referenced by foreign key on an another table in Entity Framework 实体框架6更新表并插入到外键相关表中 - Entity Framework 6 update a table and insert into foreign key related tables 实体框架:在Update表中插入外键行 - Entity Framework: Insert foreign key rows when Update table 使用实体框架,如何在两个模型上添加外键以相互引用 - Using Entity Framework, how can I add a foreign key on two models to reference each other 如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表 - How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM