This question is locked. New answers and comments are not allowed.
I'm getting a duplicate key constraint violation on a unique index while saving my db context.
Table
- Id (primary key)
- Name
- bool IsActive
... (other properites)
Constraint is that there can only be a single "Active" record of a given name. I've checked the .rlinq generated from my SqlServer database, and the index is registered as a unique index on Name. The Index in the rlinq file does not list the IsActive column in the mappings, only the Name. In SQL this part of the constraint is accomplished by a Filter where IsActive=true. Not sure if there is a way to carry that over into the DataAccess model.
Here's what i'm trying to do (without breaking it into multiple context changes).
1. Modify existing active record (of Name 'X') to be inactive.
2. Add a new record, also of Name 'X', with IsActive=true
3. SaveChanges on the context.
I can only assume that the new record is getting added before the changes to the old record are saved to the database, resulting in the violation. Is there a way I can handle this without breaking it up into multiple context changes (i.e. modify existing record, SaveChanges, add new record, SaveChanges).
Any help would be appreciated!
Table
- Id (primary key)
- Name
- bool IsActive
... (other properites)
Constraint is that there can only be a single "Active" record of a given name. I've checked the .rlinq generated from my SqlServer database, and the index is registered as a unique index on Name. The Index in the rlinq file does not list the IsActive column in the mappings, only the Name. In SQL this part of the constraint is accomplished by a Filter where IsActive=true. Not sure if there is a way to carry that over into the DataAccess model.
Here's what i'm trying to do (without breaking it into multiple context changes).
1. Modify existing active record (of Name 'X') to be inactive.
2. Add a new record, also of Name 'X', with IsActive=true
3. SaveChanges on the context.
I can only assume that the new record is getting added before the changes to the old record are saved to the database, resulting in the violation. Is there a way I can handle this without breaking it up into multiple context changes (i.e. modify existing record, SaveChanges, add new record, SaveChanges).
Any help would be appreciated!