Problem with SaveChanges

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
KevClark64
Posts: 8
Joined: Thu 12 Dec 2013 22:30

Problem with SaveChanges

Post by KevClark64 » Wed 24 Jun 2015 18:02

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

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Problem with SaveChanges

Post by Shalex » Thu 25 Jun 2015 10:58


KevClark64
Posts: 8
Joined: Thu 12 Dec 2013 22:30

Re: Problem with SaveChanges

Post by KevClark64 » Thu 25 Jun 2015 15:21

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.

KevClark64
Posts: 8
Joined: Thu 12 Dec 2013 22:30

Re: Problem with SaveChanges

Post by KevClark64 » Thu 25 Jun 2015 19:45

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.

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Problem with SaveChanges

Post by Shalex » Mon 29 Jun 2015 11:20

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.

Post Reply