C
C#2y ago
Elio

Sqlite Error 19

Hi, i've a problem i'm trying to add something to the database but this error occured when i'm using AddStep() but not when i'm trying it "manually". Have you any idea why their is an error while using the methods AddStep() ?
16 Replies
Trinitek
Trinitek2y ago
I don't see a difference in the values in step and step2 'unique constraint failed' means one or more of the columns in your schema are supposed to be...unique so naturally adding the same exact values a second time is going to error
Elio
Elio2y ago
if i remove the part that work the method AddStep() still throw the same error
Trinitek
Trinitek2y ago
oh I see, you're creating a new DbContext in the method if you're already in a context, use that instance, don't create a new one inside that way EF can track your entities correctly right now it's trying to add the same ProgramBending twice
Elio
Elio2y ago
Elio
Elio2y ago
even like this i've the error though
Trinitek
Trinitek2y ago
because ProgramBending is attached to the step, when you add the step, it also thinks the ProgramBending is new, and tries to add that you can tell EF to not mark the entity as New, or you can load the ProgramBending from EF so it's tracked correctly, and then attach that instance to the step
Elio
Elio2y ago
it seems to be that cause it don't work when i'm trying to add something from another new context
Elio
Elio2y ago
Trinitek
Trinitek2y ago
yeah so basically what you need to do is call Attach() on the ProgramBending property on your step, because it needs to be in the Unchanged state
Elio
Elio2y ago
in the modelbuilder ?
Trinitek
Trinitek2y ago
no, on the context call where you call Add()
Elio
Elio2y ago
oh yeah i just saw i will try this
Trinitek
Trinitek2y ago
the documentation also mentions this, the primary key on ProgramBending must be set, otherwise it will think it's a new entity to be added, even when you call Attach
Elio
Elio2y ago
this won't be a problem because to create dataprogram i need to create programbending before well now it work well with Attach() Thanks this was a bit tricky to me to find the solution alone
Trinitek
Trinitek2y ago
yeah the docs aren't very clear on this imo