带有 edmx 和 objectdatasource 错误的 CRUD 操作 Idisposable
我有 3 层应用程序。
在持久层中,只是拖放 Entities.edmx 文件。
在表示层中:
<asp:ObjectDataSource ID="ObjectDataSource_Tva" runat="server"
DeleteMethod="del_Tva" InsertMethod="Inst_Tva"
SelectMethod="Get_All_Tva"
TypeName="FaExped_BackEnd_WebApp_Business.Le_T.LeTVA_Entite_BL"
UpdateMethod="Updt_Tva">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Libelle" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Libelle" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
以及连接到此对象数据源的网格视图。
在我的业务逻辑层中,当我像这样使用它时:
public IEnumerable<T_TVA> Get_All_Tva()
{
FaExpedEntities oEntite_T = new FaExpedEntities();
var query = from o in oEntite_T.T_TVA select o;
IEnumerable<T_TVA> LesTva = query;
return LesTva;
}
它有效,但是当我像这样使用它时:
public IEnumerable<T_TVA> Get_All_Tva()
{
using (FaExpedEntities oEntite_T = new FaExpedEntities())
{
var query = from o in oEntite_T.T_TVA select o;
IEnumerable<T_TVA> LesTva = query;
return LesTva;
}
}
它不起作用。它表示ObjectContext
实例已被删除,无法用于需要连接的操作。
为什么?
使用“using
”语法失败和不使用“using
”有效有什么区别?
有或没有“using
”哪种方法更好?
I have 3 tiers apps.
In the Persistance layer the is just the drag 'n' drop Entities.edmx file.
In the Presentation layer :
<asp:ObjectDataSource ID="ObjectDataSource_Tva" runat="server"
DeleteMethod="del_Tva" InsertMethod="Inst_Tva"
SelectMethod="Get_All_Tva"
TypeName="FaExped_BackEnd_WebApp_Business.Le_T.LeTVA_Entite_BL"
UpdateMethod="Updt_Tva">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Libelle" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Libelle" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
And a gridview that connects to this objectdatasource.
In my busines logic layer, when I use it like this:
public IEnumerable<T_TVA> Get_All_Tva()
{
FaExpedEntities oEntite_T = new FaExpedEntities();
var query = from o in oEntite_T.T_TVA select o;
IEnumerable<T_TVA> LesTva = query;
return LesTva;
}
It works, but when I use like this:
public IEnumerable<T_TVA> Get_All_Tva()
{
using (FaExpedEntities oEntite_T = new FaExpedEntities())
{
var query = from o in oEntite_T.T_TVA select o;
IEnumerable<T_TVA> LesTva = query;
return LesTva;
}
}
It does not work. It said that the instance of ObjectContext
has been deleted and cannot be used for operations that require a connection.
Why?
What is the difference such that using "using
" syntax fails and not using "using
" works?
Which is the better approach, with or without the "using
"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑包含
using
语句的代码的错误是由于延迟执行造成的。本质上,这意味着在执行 LINQ 查询以检索结果之前,数据库连接已关闭。一种常见的解决方法是使用非延迟运算符(例如
ToList()
)“强制”立即执行 LINQ 查询。有很多好的MSDN 上有关延迟执行的文章,该延迟执行适用于所有类型的 LINQ,而不仅仅是将 LINQ 与实体框架结合使用。
I suspect your error with the code that has the
using
statement is due to deferred execution. Essentially it means that the database connection has been closed before the LINQ query is executed to retrieve the results.One common work around is to 'force' the LINQ query to be immediately executed using a non deferred operator like
ToList()
.There are plenty of good articles on MSDN regarding deferred execution which applies to all types of LINQ, not just using LINQ with Entity Framework.