I think you should not follow the first article, and I will tell you why.
Following the second approach you're loosing pretty much every feature that Entity Framework provides via the DbContext
, including its 1st-level cache, its identity map, its unit-of-work, and its change tracking and lazy-loading abilities. That's because in the scenario above, a new DbContext
instance is created for every database query and disposed immediately afterwards, hence preventing the DbContext
instance from being able to track the state of your data objects across the entire business transaction.
Having a DbContext
as a private property in your repository class also has its problems. I believe the better approach is having a CustomDbContextScope. This approach is very well explained by this guy: Mehdi El Gueddari
This article http://mehdi.me/ambient-dbcontext-in-ef6/ one of the best articles about EntityFramework I've seen. You should read it entirely, and I believe it will answer all your questions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…