Page 1 of 1

Problem with SaveChanges

Posted: Wed 24 Jun 2015 18:02
by KevClark64
I'm having a problem with SaveChanges in that sometimes it simply does not save. It's returning 0 so I know the data isn't being saved, but I'm wondering if there is any way to get more information about what the error actually is.

Kevin Clark

Re: Problem with SaveChanges

Posted: Thu 25 Jun 2015 10:58
by Shalex

Re: Problem with SaveChanges

Posted: Thu 25 Jun 2015 15:21
by KevClark64
I appreciate the link, but the data on that page doesn't really solve my problem, for two reasons:

1) When SaveChanges fails, it doesn't generate a catchable error as far as I can tell, so I don't know what the error is.
2) Refreshing the context doesn't seem to help.

Re: Problem with SaveChanges

Posted: Thu 25 Jun 2015 19:45
by KevClark64
I'm still investigating this, but I think I may have found the issue. It may be that SaveChanges is returning 0 when the data to be saved is not different from the existing data. In other words, I'm grabbing a record, updating some values, and then calling SaveChanges. But the updated data may be the same data that already was in the record.

Re: Problem with SaveChanges

Posted: Mon 29 Jun 2015 11:20
by Shalex
KevClark64 wrote:I'm still investigating this, but I think I may have found the issue. It may be that SaveChanges is returning 0 when the data to be saved is not different from the existing data. In other words, I'm grabbing a record, updating some values, and then calling SaveChanges. But the updated data may be the same data that already was in the record.
If the WHERE clause of the generated SQL finds the record (even if the update doesn't make changes because the new value is equal to the existing one), SaveChanges() returns the number of rows found. I have run the following code:

Code: Select all

        static void Main(string[] args)
        {
            var monitor = new Devart.Data.PostgreSql.PgSqlMonitor() { IsActive = true };

            int first = 0;
            int second = 0;

            using (var context = new PostgreModel.PostgreEntities())
            {
                var dept = context.Depts.Where(a => a.Deptno == 10).FirstOrDefault();
                dept.Dname = "development";
                first = context.SaveChanges();
            }

            using (var context2 = new PostgreModel.PostgreEntities())
            {
                var dept2 = context2.Depts.Where(a => a.Deptno == 10).FirstOrDefault();
                dept2.Dname = "development";
                second = context2.SaveChanges();
            }
        }
The result value of both "first" and "second" is 1.

If you are experiencing a different behaviour, please localize the issue and send us a small test project with the corresponding DDL/DML script for reproducing.