Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架savechanges()更新删除插入_Entity Framework_Insert Update_Savechanges - Fatal编程技术网

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

Entity framework 实体框架savechanges()更新删除插入,entity-framework,insert-update,savechanges,Entity Framework,Insert Update,Savechanges,我希望我的SaveChanges()函数更新数据库中的一条记录,如果返回值为“1”(来自存储过程),则不应执行“delete”命令(存储过程)。 现在,问题是db.SaveChanges()(ObjectContext的一个实例)正在成功地更新我的记录,但在更新之后,它执行delete命令。我应该如何告诉我的函数不要执行delete命令 using (var db = new PRLAdminEntities()) { bool isExi

我希望我的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;

            }
        }
使用(var db=new prladminenties())
{
bool-isExists=false;
string lastExisting=string.Empty;
string errorString=string.Empty;
db.Connection.Open();
trans=db.Connection.BeginTransaction();
//要发送回客户端的帐户
var countriesToSendBack=新列表();
//处理每个需要更新数据库的帐户
if(request.CountriesToUpdate!=null)
{
foreach(请求中的var国家/地区。CountriesToUpdate)
{
//发送回的国家/地区。删除(国家/地区);
var temp=来自db.Countries中的行,其中row.Name.ToUpper()==country.Name.ToUpper()选择行;
if(temp.Count()>0&&country.ChangeTracker.State==ObjectState.Added)
{
Countries返回。添加(国家);
db.Countries.ApplyChanges(国家);
isExists=真;
lastExisting=country.Name;
errorString+=country.Name+“,”;
//db.GetAllCountries();
//中断;
继续;
}
if(country.ChangeTracker.State==ObjectState.Deleted)
{
db.DeleteObject(国家);
}
//如果更改或修改(不是删除)
if(country.ChangeTracker.State!=ObjectState.Deleted)
{
//此帐户应被发回
如果(!countriesToSendBack.Contains((国家)))
Countries返回。添加(国家);
如果(country.Active==false)
{
db.Countries.ApplyCurrentValues(国家);
}
}
//应用所有更改
db.Countries.ApplyChanges(国家);
}
如果(存在)
{
//回答:成功=错误;
//errorString.Replace(“,“+lastExisting+”,“,”&“+lastExisting”);
//response.FaultMessage=“重复记录”;
}
}
//保存所有更改
int total=db.SaveChanges();
回答:成功=正确;
foreach(变量countryItem在countriesToSendBack中)
{
countryItem.Id=(从db.Countries中的行开始,其中row.Name.ToUpper()==countryItem.Name.ToUpper()选择row.Id);
}
trans.Commit();
//刷新获取时间戳等的帐户数据
db.Refresh(RefreshMode.StoreWins,countriesToSendBack);
//设置响应值
响应。国家=国家发送回;
}
}

也许我误解了你的问题,我不完全明白你想做什么

但是为什么不在更改之后调用SaveChanges(),并且当所有检查都为正值时,执行remove()并再次调用SaveChanges()


多次调用SaveChanges()并没有什么坏处。它会将其数据镜像到您的数据库。如果执行删除操作,则将尝试在数据库中删除它。这是一件好事。。它做你让它做的;-)

是的,但我如何告诉它进行更新,如果成功完成,则不进行删除或删除?没有伤害?您正在调用写入数据,然后将其删除(没有坏处吗?)@斯卡克:你不能。更新后,您应该从实体列表中删除该对象。@rudym:如果这是您想要的,就不要删除。谢谢。还有一个问题。为什么人们认为当你不能做你想做的事时,它是有用的?