I want my SaveChanges() function to update a record in my database and if the return value is '1' which is coming from my stored procedure then and only then 'delete' command (stored procedure) should not be executed. Now, the problem is db.SaveChanges() (an instance of ObjectContext) is updating my record successfully but after updating, it executes the delete command. How should I tell my function that not to execute delete command.

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;

            }
        }
有帮助吗?

解决方案

Perhaps I misread your question, I do not totally get what you are trying to do.

But why not call SaveChanges() after the change and when all checks are positive perform a remove() and call savechanges() again?

There is no harm is calling SaveChanges() multiple times. It will mirror it's data to your database. If you perform a remove it will try to delete it in your database. That's the nice thing about it.. it does what you tell it to do ;-)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top