我正在使用Entity Framework和ASP.NET MVC编写应用程序,因此我决定将DAL与MVC Web应用程序分开.实际上一切都很好,除了下一个问题(如果它的问题):我不得不将实体连接字符串从DAL项目的app.config复制到Web应用程序的Web.config.实际上它看起来是正确的:DAL不应该知道连接字符串到Data,它是Main Application的职责.
但我真的不喜欢,我必须在DAL的app.config中保留Entity连接字符串,因为它需要更新我的EntityModel(*.edmx).是否有一些最佳实践来引用Web应用程序的连接字符串?或者我应该像现在一样离开它?
花了很多时间,但仍然无法理解如何避免在DbContext中缓存.
我附上了一些简单案例的实体模型,以证明我的意思.
问题是dbcontext缓存结果.例如,我有下一个代码来查询我的数据库中的数据:
using (TestContext ctx = new TestContext())
{
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select new
{
b,
flats = from f in b.Flats
select new
{
f,
people = from p in f.People
where p.Archived == false
select p
}
}).AsEnumerable().Select(x => x.b).Single();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,一切都很好:我得到了我想要的东西(只有Archived == false的人).
但是,如果我加入后,另一个查询,例如,查询有已归档的标志设置为true人的建筑,我接下来的事情就,我真正明白水湿:
这个查询的代码是下一个:
using (TestContext ctx = new TestContext())
{
var res = (from b in ctx.Buildings.Where(x => x.ID == 1)
select …Run Code Online (Sandbox Code Playgroud)