I have a EF DbContext which I registered in my Autofac container:
builder.RegisterType<Data.Foobar>().As<IFoobarContext>().InstancePerDependency();
This object is being used in other objects which I also registered in my Autofac container:
builder.RegisterType<InteractionManager>().As<IInteractionManager>().SingleInstance();
Now I'm getting a error once in a while after updating the database:
Exception: System.Data.SqlClient.SqlException Message: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ParticipantCode_Participant". The conflict occurred in database "database", table "dbo.Participant", column 'Id'. The statement has been terminated.
After a IIS reset this error vanishes and the same action will run without any exceptions. I know for sure that the FK is correct.
After some research it looked like the dbcontext could be in an inconsistent state. I added the following to my Data.Foobar registration:
builder.RegisterType<Data.Foobar>().As<IFoobarContext>().OnActivating(x =>
{
x.Instance.Dispose();
x.ReplaceInstance(new Data.Foobar());
})
.InstancePerDependency();
This does not work either.
It is a really strange issue with not much to go on so that is why I am asking all of you. Could it be that because my InteractionManager is a singleinstance, my dbcontext is also and the database state is not being updated correctly?