实体框架savechanges()更新删除插入

时间:2014-02-19 09:57:44

标签: entity-framework insert-update savechanges

我希望我的SaveChanges()函数更新我的数据库中的记录,如果返回值是'1',它来自我的存储过程,那么只有'delete'命令(存储过程)不应该被执行。 现在,问题是db.SaveChanges()(ObjectContext的一个实例)正在成功更新我的记录但是在更新之后,它执行delete命令。我该怎么告诉我的函数不要执行delete命令。

using (var db = new PRLAdminEntities())
            {
                bool isExists = false;
                string lastExisting = string.Empty;
                string errorString = string.Empty;

                db.Connection.Open();
                trans = db.Connection.BeginTransaction();
                //accounts to be sent back to client
                var countriesToSendBack = new List<Polo.Common.Shared.Entities.Country>();

                //process each account requiring database update
                if (request.CountriesToUpdate != null)
                {
                    foreach (var country in request.CountriesToUpdate)
                    {
                        //countriesToSendBack.Remove(country);

                        var temp = from row in db.Countries where row.Name.ToUpper() == country.Name.ToUpper() select row;
                        if (temp.Count<Polo.Common.Shared.Entities.Country>() > 0 && country.ChangeTracker.State == ObjectState.Added)
                        {

                            countriesToSendBack.Add(country);
                            db.Countries.ApplyChanges(country);

                            isExists = true;
                            lastExisting = country.Name;
                            errorString += country.Name + ", ";
                            //db.GetAllCountries();
                            //break;
                            continue;

                        }
                        if (country.ChangeTracker.State == ObjectState.Deleted)
                        {
                            db.DeleteObject(country);
                        }

                        //if a change or modification (not a delete)
                        if (country.ChangeTracker.State != ObjectState.Deleted)
                        {
                            //this account should be sent back

                            if (!countriesToSendBack.Contains((country)))
                                countriesToSendBack.Add(country);
                            if (country.Active == false)
                            {
                                db.Countries.ApplyCurrentValues(country);

                            }

                        }
                        //apply all changes

                        db.Countries.ApplyChanges(country);
                    }
                    if (isExists)
                    {


                        //response.Success = false;
                        //errorString.Replace(", " + lastExisting + ",", " & " + lastExisting);
                        //response.FaultMessage = "Duplicate Records";

                    }
                }

                //save all changes
                int total = db.SaveChanges();
                response.Success = true;
                foreach (var countryItem in countriesToSendBack)
                {
                    countryItem.Id = (from row in db.Countries where row.Name.ToUpper() == countryItem.Name.ToUpper() select row.Id).FirstOrDefault();
                }
                trans.Commit();

                //refresh the account data which gets timestamp etc
                db.Refresh(RefreshMode.StoreWins,countriesToSendBack);

                //set the response values
                response.Countries = countriesToSendBack;

            }
        }

1 个答案:

答案 0 :(得分:1)

也许我误解了你的问题,我并没有完全明白你想要做的事情。

但是为什么不在更改后调用SaveChanges()并且当所有检查都是肯定时执行remove()并再次调用savechanges()?

多次调用SaveChanges()没有任何害处。它会将数据镜像到您的数据库。如果您执行删除操作,尝试在数据库中删除它。这是关于它的好东西..它做你告诉它做的事情; - )

相关问题