I am working on an ASP.NET MVC website, and am using Entity Framework to save/update data into my database.
I am using ninject to inject dbContext into my repository class like below:
public class MyRepository : IMyRepository
{
MyContext _context;
public MyRepository(MyContext context)
{
_context = context;
}
// more code ...
This is how I am injecting the context using ninject:
kernel.Bind<MyContext>().ToSelf().InSingletonScope();
And I am using the injected dbContext as below to save the data into my database:
public void InsertModel(MyModel r)
{
_context.MyModel.Add(r);
_context.SaveChanges();
}
Now, the problem I am facing is that if an exception occurs (for example if I purposely pass an invalid Model), Entity Framework will throw an exception. The invalid entity remains in the dbContext and all further save attempts will fail because of that.
I have been googling this issue and found This Article which suggests putting save / update operation in a try / catch block. If any exception occurs, it suggests to clear dbContext in the catch block.
Is this the correct appraoch? It sounds like a lot of work to put a try / catch around every save operation and clear the dbContext? I Would appreciate any help on explaining the correct approach for clearing the context.