ASP.NET应用程序的实体框架注意事项 [英] Entity Framework considerations for ASP.NET applications

查看:45
本文介绍了ASP.NET应用程序的实体框架注意事项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了具有数据库模型的业务层,该数据库模型将在ASP.NET应用程序中使用.我以前在Windows窗体中使用过Linq To SQL类,但是在按请求的Web应用程序中使用ORM对我来说是陌生的.我有一些我不知道的事情, 如果有人给我任何见解,我将不胜感激.

我的BLL具有静态方法,例如GetRecord()或UpdateRecord().这些方法中的每一个都创建一个新的ObjectContext实例,该实例在工作单元之后销毁.我没有任何HttpContext.Current.Items缓存实现.

我正在使用EF .NET 3.5.

1.我已经创建了一个预生成的视图(Model.View.cs)并将其添加到我的解决方案中.这是我要做的所有事情吗?还需要用我的dll发布csdl,msl和ssdl文件吗?

2.预编译查询是否对ASP.NET应用程序有害?我只喜欢对一个ASPX页面进行一两个查询,很少有在同一页面中使用两次的选择查询.如果预编译我的查询,它会减慢应用程序的速度吗?我想知道是否进行了预编译 通过会议A对会议B有用吗?

3.我创建了以下方法来更新ASP.NET页中的记录,但我想知道这是否是一种很好的方法:

  ASP.NET使用BLL.GetRecord()获取记录(实体)
更新任何值
将更新的记录发送到BLL.Update()
BLL.Update()检查记录是否存在
使用context.ApplyPropertyChanges()更新记录

4.我绘制了一些实体框架性能图表,并且在每个图表中都有两种不同的查询统计信息:第一次运行和第二次运行.由于我使用工作单元类型的设计,因此我的查询永远不会看到第二次运行吗?


谢谢.

I've created a business layer with a database model to be used in an ASP.NET application. I've used Linq To SQL Classes in Windows Forms before but using ORMs in per-request web applications is foreign to me. I've a few things I couldn't figure out and I'd appreciate if anyone give me any insight..

My BLL has static methods like GetRecord() or UpdateRecord(). Each one of these methods creates a new ObjectContext instance, destroyed after unit of work. I don't have any HttpContext.Current.Items cache implementation.

I'm using EF .NET 3.5.

1. I've created a pre-generated view (Model.View.cs) and added it to my solution. Is this all I have to do to use it? Also do I need to publish csdl, msl and ssdl files with my dll?

2. Is precompiling queries bad for ASP.NET applications? I have like only one or two queries for any ASPX page and very rarely a select query used twice in the same page. Will it slow down the application if precompile my queries? I wonder if a precompile made by Session A would be useful for Session B?

3. I've created the following method to update a record in ASP.NET page and I wonder if it is a good way to do it:

 ASP.NET gets the record(Entity) using BLL.GetRecord()
Updates any values
Sends updated record to BLL.Update()
BLL.Update() checks if the record exists
Uses context.ApplyPropertyChanges() to update the record

4. I've red a few entity framework performance charts and in every one of those charts there are two different statistics for queries: first run and the second run. Since I work with unit-of-work type of design, will my queries never see second runs?


Thanks.

推荐答案

1.是的,在生成的视图文件中有一个EntityViewGenerationAttribute,它指定包含预生成视图的类的名称.生成视图不会影响将CSDL,MSL和SSDL文件保存在 实体框架可以找到它们.通常,如果通过在其中一个程序集中包含EDMX文件来保持默认行为,它将在该程序集中嵌入CSDL,MSL和SSDL文件.

1. Yes, in the generated views file there's a EntityViewGenerationAttribute that specifies the name of the class that contains the pre-generated views. Having generated views doesn't affect the need to keep the CSDL, MSL, and SSDL files in a place that the Entity Framework can find them. Usually if you keep the default behavior by having an EDMX file in one of your assemblies, it will embed the CSDL, MSL, and SSDL files inside of that assembly.

2.在ASP.NET应用程序中预编译查询应该不会有任何问题.通常,在进行预编译查询时,您会将编译结果缓存在一个静态变量中,因此,您只需在应用程序的生存期内支付一次编译费用, 不管当前会话如何.

2. There shouldn't be any problems with precompiling queries in ASP.NET applications. Usually when precompiling queries you cache the compilation result in a static variable, so you only pay the cost of compilation once in the lifetime of the application, regardless of the current session.

3.由于业务层中的每个方法都会创建ObjectContext的新实例,因此在进行更新时有很多挫败的机会.在用于更新记录的工作流中,BLL.Update将需要获取原始记录,因此ApplyPropertyChanges 实际上会为该上下文更新某些内容.这意味着您将获得两次记录.

3. Since each method in the business layer creates a new instance of the ObjectContext, there is ample chance for frustration when doing updates. In your workflow for updating records, BLL.Update would need to get the original record so ApplyPropertyChanges would actually update something for that context. This means you're getting the record twice.

相反,如果确实将上下文保留在HttpContext.Current.Items中,那么您要做的就是在GetRecord中检索记录,更新值,然后完成.尽管如果您不希望业务层依赖于HttpContext,这可能会很困难.

Instead if you do keep the context in HttpContext.Current.Items, then all you need to do is retrieve the record in GetRecord, update the values and you're done. Although this can be difficult if you don't want your business layer to depend on HttpContext.

4.所述首次运行"步骤包括:每个AppDomain for EF都会发生一次以加载元数据(来自CSDL,SSDL和MSL文件)的情况,因此无论会话如何,您都将看到第二次运行"除了第一个查询以外的所有查询.

4. The "first run" happens once per AppDomain for EF to load the metadata (from the CSDL, SSDL, and MSL files), so regardless of sessions, you will see the "second run" on all queries but the first one.

希望有帮助.如果您还有其他问题,请告诉我.

Hope that helps. Let me know if you have further questions.

David


这篇关于ASP.NET应用程序的实体框架注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆