In my ASP.NET MVC 4 web application, I have utilized partial views and custom display/editor templates to modularize the code. One example is a User.cshtml DisplayTemplate which takes User (an Entity object) and prints out their name and an icon to popup their directory info.
在我的ASP.NET MVC 4 Web应用程序中,我使用了部分视图和自定义显示/编辑器模板来模块化代码。一个例子是User.cshtml DisplayTemplate,它接受User(一个Entity对象)并输出他们的名字和一个图标来弹出他们的目录信息。
Views/Shared/DisplayTemplates/User.cshtml
@model MyApp.Domain.Entities.User
@if (Model != null) {
@Html.DisplayFor(m => m.DisplayName)
<span class="view-contact icon ui-icon ui-icon-contact" title="View Contact"></span>
@Html.HiddenFor(m => m.Identity)
}
I'm having the opposite problem most people seem have when I searched on the topic. I've noticed that when I use this template, lazy loading is triggered and so a query is sent to the DB to grab the data, but I don't want this to happen if I've already preloaded the data, especially in the case where I show a listing of users. In that case I made sure to use .Include("User") in my query and the info displays without issue or additional querying when I essentially write out the template's code in the view:
当我搜索这个主题时,我遇到了大多数人似乎遇到的相反问题。我注意到,当我使用这个模板时,会触发延迟加载,因此会向数据库发送查询以获取数据,但如果我已经预加载了数据,我不希望这种情况发生,特别是在我显示用户列表的情况。在这种情况下,我确保在我的查询中使用.Include(“User”),当我基本上在视图中写出模板的代码时,信息显示没有问题或额外的查询:
Simplified excerpt from Views/MyController/List.cshtml
来自Views / MyController / List.cshtml的简化摘录
...
@foreach (var item in Model) {
<tr>
<td class="alignl">
@item.User.DisplayName
<span class="view-contact icon ui-icon ui-icon-contact" title="View Contact"></span>
@Html.HiddenFor(m => item.User.Identity)
</td>
</tr>
}
...
If I replace those three lines with the call to template, each line queries the db.
如果我用模板调用替换这三行,每行都会查询数据库。
@foreach (var item in Model) {
<tr>
<td class="alignl">
@Html.DisplayFor(i => item.User)
</td>
</tr>
}
How do I utilize this template without triggering a unnecessary query?
如何在不触发不必要查询的情况下使用此模板?
1 个解决方案
#1
0
Based on @GertArnold 's suggestion I looked a little closer at the main query that grabs the collection of users and tried to see if I could disable lazy loading. Because I was using a generic repository pattern and unit of work, I wasn't able to specifically disable it just for that call so I ended up using the context directly and everything behaved properly without disabling needed -- this breaks my pattern, but I now have the benefit of being able to use projection and save my application from eagerly loading unnecessary data columns.
根据@GertArnold的建议,我仔细查看了抓取用户集合的主要查询,并试图查看是否可以禁用延迟加载。因为我使用的是通用存储库模式和工作单元,所以我无法专门为该调用禁用它,所以我最终直接使用了上下文,所有操作都正常而不需要禁用 - 这打破了我的模式,但我现在的好处是能够使用投影并保存我的应用程序,从而急切地加载不必要的数据列。
If anyone can point me down the right path for implementing projection in a generic repository, I'd be greatful, but I can live with this alternative solution for now.
如果有人能指出我在通用存储库中实现投影的正确道路,我会很高兴,但我现在可以忍受这种替代解决方案。
This msdn article also helped: https://msdn.microsoft.com/en-us/data/jj574232.aspx
这篇msdn文章也有所帮助:https://msdn.microsoft.com/en-us/data/jj574232.aspx
#1
0
Based on @GertArnold 's suggestion I looked a little closer at the main query that grabs the collection of users and tried to see if I could disable lazy loading. Because I was using a generic repository pattern and unit of work, I wasn't able to specifically disable it just for that call so I ended up using the context directly and everything behaved properly without disabling needed -- this breaks my pattern, but I now have the benefit of being able to use projection and save my application from eagerly loading unnecessary data columns.
根据@GertArnold的建议,我仔细查看了抓取用户集合的主要查询,并试图查看是否可以禁用延迟加载。因为我使用的是通用存储库模式和工作单元,所以我无法专门为该调用禁用它,所以我最终直接使用了上下文,所有操作都正常而不需要禁用 - 这打破了我的模式,但我现在的好处是能够使用投影并保存我的应用程序,从而急切地加载不必要的数据列。
If anyone can point me down the right path for implementing projection in a generic repository, I'd be greatful, but I can live with this alternative solution for now.
如果有人能指出我在通用存储库中实现投影的正确道路,我会很高兴,但我现在可以忍受这种替代解决方案。
This msdn article also helped: https://msdn.microsoft.com/en-us/data/jj574232.aspx
这篇msdn文章也有所帮助:https://msdn.microsoft.com/en-us/data/jj574232.aspx