SQLite Constraint Violation

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
jparis
Posts: 3
Joined: Wed 03 Apr 2013 14:18

SQLite Constraint Violation

Post by jparis » Wed 03 Apr 2013 14:42

Hey all, posting here, though this may not necessarily be a Devart dotConnect specific issue. I really have no idea what is causing this and I was hoping someone could help.

I have two models that 'sort-of' look like this:

public abstract class Root
{
public Guid ID {get;set;}
}

public class User : Root
{}

public class SomeObject : Root
{
public virtual User Owner {get;set;}
}

If I add a user to my database, save changes, create 'SomeObject' set the Owner to the User that I added, then add my new 'SomeObject' instance to the database, I get the following when calling SaveChanges:

SQLiteException
"Abort due to constraint violation column ID is not unique."

Is this due to the fact that I have an ID for my SomeObject instance, then an ID for my User instance that the SomeObject entity is trying to keep track of? Shouldn't this be handled for me?

Anyway, any help would be greatly appreciated.

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

Re: SQLite Constraint Violation

Post by Shalex » Thu 04 Apr 2013 07:08

Please turn on the dbMonitor tool in your application:
http://www.devart.com/dotconnect/sqlite ... nitor.html
http://www.devart.com/dbmonitor/dbmon3.exe

You will find out which DDL is generated for creating tables and corresponding DML statements with values of parameters - for inserting data. This should help to identify the reason of the problem. Notify us about the result.

jparis
Posts: 3
Joined: Wed 03 Apr 2013 14:18

Re: SQLite Constraint Violation

Post by jparis » Thu 04 Apr 2013 09:12

That was great, thanks. It looks like EntityFramework was trying to re-add a virtual reference entity.

I added the following which seemed to solve the problem:

context.Entry(((SomeObject)item).Owner).State = EntityState.Unchanged;

Thanks again.

Post Reply