entity-framework - Entity Framework 4 : How expensive is it to create an EntityConnection?

标签 entity-framework entity-framework-4

创建 EF4 EntityConnection 的成本如何?我正在使用 SQL Compact 创建一个 EF4 桌面应用程序,用户将能够使用“文件打开”对话框打开数据库文件。然后我的代码构建一个 EntityConnection,如下所示:

// Configure a SQL CE connection string  
var sqlCompactConnectionString = string.Format("Data Source={0}", filePath);

// Create an Entity Connection String Builder
var builder = new EntityConnectionStringBuilder();

// Configure Builder
builder.Metadata = string.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", m_EdmName);
builder.Provider = "System.Data.SqlServerCe.4.0";
builder.ProviderConnectionString = sqlCompactConnectionString;
var edmConnectionString = builder.ToString();

// Create an EDM connection
var edmConnection = new EntityConnection(edmConnectionString);

我有一个 ObjectContextFactory 类,它根据需要为 Repository 类创建对象上下文。

所以,这是我的问题:在我初始化工厂时构建一次 EntityConnection 是更好的做法,还是工厂每次创建对象上下文时都应该构建一个新连接?感谢您的帮助。

最佳答案

据我所知,EF4 中的开销很小 - 请验证 here - 基本上它归结为打开一个新的数据库连接,如果提供者支持连接池(SQL 服务器支持连接池),即使这个成本也很小。

来自 MetadataWorkspace 的元数据被全局缓存,因此这不会降低性能(在 2009 年发布链接到另一篇博文的博文时可能并非如此)。

此外,配置文件中的连接字符串(该博文中指出的其他性能问题)都缓存在内存中,因此我也看不出这会对性能产生怎样的负面影响。

我肯定会为每个工作单元使用一个新的实体连接。

关于entity-framework - Entity Framework 4 : How expensive is it to create an EntityConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425516/

相关文章:

C# 列表框绑定(bind)到实体 "Entity Framework"

c# - 如何应用来自 "Before"的迁移

c# - 实现业务逻辑验证的最佳实践 - Entity Framework

entity-framework - 如何在运行时从 DbContext 获取表名

c# - 根据表名获取表(ObjectSet)( Entity Framework )

.net - 检测 Entity Framework EntityObject 上的哪些属性发生了变化

entity-framework-4 - 使用 Ninject、Entity Framework 4 Code-First CTP 5、模式的 ASP.NET MVC 3 应用程序

.net - 实体以不正确的顺序保存而没有暴露外键

entity-framework-4 - Entity Framework 正在尝试创建数据库,但不是我想要的

c# - 如何使用 Entity Framework 将空值传递给存储过程?