c# - How do I set my project to create the entity-framework context per http request? -
I am using EF6 on the C # MVC5 project. This reference is currently made in Unity Config .cs class, which is called from application_start () in the world to see the use of some unity config class below:
Register the public static void () {Var Container = New Unity Container (); BlogSite.Domain.BlogSiteModelContainer Reference = New Domain. BlogSiteModelContainer (); Container. Registration Type & lt; IBlogRepository, BlogRegository & gt; (New injection congestor); }
The problem with this is that the database can be updated directly and reference to EF will be inconsistent. So I need to change it so that every time a new http request can be made.
I've found that PerRequestLifetimeManager might be the solution for this, however, I can not find any good examples how I should use it.
How should I go about implementing it? Or is there a better way?
So I have tried some things but have not been found anywhere yet. I realized I could add a container to a Lifetime manager. Registry Type ()
method, but I think it applies only to the repository, not the unit framework context.
For example
container. Registration Type & lt; IBlogRepository, BlogRegository & gt; (New PerRequestLifetimeManager (), new injection congestion (reference));
I still can not believe how the lifetime of the reference was set. I realized that I would have to reformat the code a bit, but I can not get an example of this situation anywhere.
I am doing more research, and I have seen the idea of creating a unit of work for the reference and passing it to the repository is not sure its best method is forwarded , But it seems that he could do the job. Any thoughts on that?
Found the solution I need to create a unitoffer class, under which I have created the references and conservatories. I then used Unity to inject the UnitOfWork class into Controllers with the PerRequestLifetimeManager lifetime manager.
Then my unitoffer class (which is stored in my DAL project) is:
Public class unitAuthor: IDisposable {Private Static BlogSite.Domain.BlogSiteModelContainer Reference; Public IBlogRepository Blog Repository; Public Universe () {context = new BlogSite.Domain.BlogSiteModelContainer () ;; BlogRepository = New blog archipelago (reference); } Save Public Zero () {context.SaveChanges (); } Private boole dealt = false; Protected Virtual Wide Disposing (If (DISSPOZED!!) (If Dissection) {context.Dispose ();}} This.disposed = true;} Public Zero Extract () (Dispose (true); GC.SuppressFinalize (This);}}
And I just register it with my Unity Configuration class with the following:
Container. Registry Instance & lt; UnitOfWork & gt; (New Unitofwork), New PerRequestLifetimeManager ());
In my controllers, I have a construct There were some minor changes to make, so that they could accept the unitofofficial instead of the repository.
Example
IBlogRepository blogRepo; Public BlogController (UnitOfficerUntofOffork) {blogRepo = UnitOfWork.blogRepository;}
Comments
Post a Comment