Advertisement
Shimmy

Untitled

May 3rd, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System.Data.Entity;
  2.  
  3. namespace CommonLibrary.Database
  4. {
  5.     public abstract class DbContextLocator<TContext> where TContext : DbContext, new()
  6.     {
  7.         private TContext _dbContext;
  8.  
  9.         public TContext Current
  10.         {
  11.             get { return _dbContext; }
  12.         }
  13.  
  14.         public DbContextLocator()
  15.         {
  16.             _dbContext = GetNew();
  17.         }
  18.  
  19.         public virtual void Reset()
  20.         {
  21.             _dbContext.Dispose();
  22.             _dbContext = GetNew();
  23.         }
  24.  
  25.         protected virtual TContext GetNew()
  26.         {
  27.             return Activator.CreateInstance<TContext>();
  28.         }
  29.     }
  30. }
  31.  
  32. //Concrete class
  33. using System.Data.Entity;
  34. using CommonLibrary.Database;
  35. using ExperimentBase.EntityModels;
  36.  
  37. namespace MainProject.Models    
  38. {
  39.     public class MainDbContextLocator : DbContextLocator<MainDbContext>    {    }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement