Entity Framework 4 和 SQL Server 应用程序角色 - 如何协同工作?

发布于 2024-09-19 06:23:17 字数 697 浏览 1 评论 0原文

我正在研究 EF4,用于使用 .NET v4、EF4 和 .NET 的新内部应用程序开发项目。 SQL Server 2008 R2。迄今为止,我们的小型开发团队只进行了很少的 .NET 开发,仅演示了 EF 应用程序。我们当前的应用程序使用数据库应用程序角色来确保安全,这对我们来说效果很好。

通过阅读和一些基本的实验,我的理解是:

  • EF可以根据需要打开和关闭数据库连接。不过,可以手动打开和关闭 EF ObjectContext 使用的 EntityConnection。

  • SQL Server 应用程序角色安全性要求在数据库连接上运行 sp_setapprole 来设置应用程序角色上下文。 sp_unsetapprole 可用于将连接恢复到其原始上下文。

  • 默认情况下,数据库连接是池化的。如果连接在返回池之前未恢复到其原始上下文,则将 sp_setapprole 与连接池一起使用可能会出现问题。

如果以上所有内容均正确,那么将 EF4 与应用程序角色一起使用的明显方法是手动打开 &关闭 EntityConnection,确保在打开后执行 sp_setapprole,在关闭前执行 sp_unsetapprole。

有更好的方法吗? 我最担心的是在没有先调用 sp_unsetapprole 的情况下意外关闭连接。似乎是一个可能不会立即被注意到的错误。

I’m researching EF4 for a new in-house application development project using .NET v4, EF4, & SQL Server 2008 R2. To date, our small dev team has done very little .NET development and only demonstration EF applications. Our current applications use DB app-roles for security and that's worked out well for us.

From reading and some basic experimentation, my understanding is:

  • EF can open and close DB connections as needed. However it is possible to manually open and close an EntityConnection for use by the EF ObjectContext.

  • SQL Server app-role security requires running sp_setapprole on DB connections to set the application role context. sp_unsetapprole can be used to revert a connection to its original context.

  • By default, DB connections are pooled. Using sp_setapprole with connection pooling can be problematic if the connections are not restored to their original context before being returned to the pool.

If all the above is correct then the obvious way to use EF4 with app-roles is to manually open & close the EntityConnection, being sure to execute sp_setapprole after opening and sp_unsetapprole before closing.

Is there a better way? I'm mostly concerned about accidentally closing the connection without first calling sp_unsetapprole. Seems like an error that may not be noticed immediately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-09-26 06:23:18

您只需添加“Pooling=false;”即可到 app.config 中的商店连接(提供商连接字符串)。
如果您实际上不需要池,这似乎是最简单的解决方案。

You can just add "Pooling=false;" to your store connection in the app.config (Provider Connection String).
If you don't actually need pooling, this seems to be the simplest solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文