Last active
December 15, 2015 22:19
-
-
Save yemrekeskin/5331948 to your computer and use it in GitHub Desktop.
Using Context Factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IContextFactory | |
| { | |
| SampleContext GetContext(); | |
| } | |
| public class ContextFactory | |
| :IContextFactory,IDisposable | |
| { | |
| private SampleContext _sampleContext; | |
| public SampleContext GetContext() | |
| { | |
| return _sampleContext ?? (_sampleContext = new SampleContext()); | |
| } | |
| public void Dispose() | |
| { | |
| if (null != _sampleContext) | |
| _sampleContext.Dispose(); | |
| } | |
| } | |
| public class SampleContext | |
| :DbContext | |
| { | |
| public DbSet<Category> Category { get; set; } | |
| public SampleContext(string contextName) | |
| :base(contextName) | |
| { | |
| } | |
| public SampleContext() | |
| :base("") | |
| { | |
| } | |
| public void Commit() | |
| { | |
| base.SaveChanges(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment