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
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
if i remove the part that work the method
AddStep()
still throw the same erroroh 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
even like this i've the error though
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
Explicitly Tracking Entities - EF Core
Explicitly tracking entities with DbContext using Add, Attach, Update, and Remove
it seems to be that cause it don't work when i'm trying to add something from another new context
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
statein the modelbuilder ?
no, on the context call where you call Add()
oh yeah i just saw i will try this
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
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 aloneyeah the docs aren't very clear on this imo