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
Problem with SaveChanges
-
- Posts: 8
- Joined: Thu 12 Dec 2013 22:30
Re: Problem with SaveChanges
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.
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.
-
- Posts: 8
- Joined: Thu 12 Dec 2013 22:30
Re: Problem with SaveChanges
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
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: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.
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();
}
}
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.