加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 扩展实体框架应用程序/多个应用程序到同一个数据

发布时间:2020-12-16 09:53:28 所属栏目:asp.Net 来源:网络整理
导读:我有一个使用MVC / EF Code First编程的应用程序.它做了很多服务器端处理,并且资源非常密集. 我知道如何设置负载平衡,但是,我想知道扩展EF应用程序是否像配置新服务器,部署应用程序并指向数据库集群一样简单 – 或者我将面临任何问题多个EF应用程序命中同一
我有一个使用MVC / EF Code First编程的应用程序.它做了很多服务器端处理,并且资源非常密集.

我知道如何设置负载平衡,但是,我想知道扩展EF应用程序是否像配置新服务器,部署应用程序并指向数据库集群一样简单 – 或者我将面临任何问题多个EF应用程序命中同一个数据库服务器?

我似乎无法找到任何建议/指导,我担心我选择EF更简单/更直接的做法做出了错误的选择!

解决方法

… issues … regards to multiple EF applications hitting the same database server?

回顾一下您的应用程序是基于ASP .NET MVC的应用程序这一事实.拥有它的多个实例可能会引起国家管理的幽灵.

MSDN对why this is an issue有一个很好的介绍:

HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session,and provides a way to persist variable values for the duration of that session. By default,ASP.NET session state is enabled for all ASP.NET applications.

Alternatives to session state include the following:

  • Application state,which stores variables that can be accessed by all users of an ASP.NET application.

这一点是存储状态的一种非常常见的方式,但是当涉及多个应用程序实例时(该状态仅对其中一个实例“可见”)会中断.

通常,这可以通过使用SessionStateMode的StateServer或SQLServer值来解决.同一篇文章提供了每个选项的很好的总结(强调我的).

  • StateServer mode,which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.

  • SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.

如果您的申请是无国籍的,这是一个有争议的问题.

I am worrying I made the wrong choice by choosing EF

对于访问数据库的应用程序的多个实例的问题,您将遇到任何类型的数据访问技术问题.

以下是基本方案:假设您的应用程序按计划向用户发送欢迎电子邮件.

鉴于表用户:

UserId | Email           | WelcomeLetterSent
-------+-----------------+------------------
     1 | user@domain.com | 0

还有一些伪代码:

foreach (var user in _context.Users.Where(u => !u.WelcomeLetterSent))
{
    SendEmailForUser(user);
    user.WelcomeLetterSent = true;
}

_context.SaveChanges();

有一个race condition,你的应用程序的实例1和实例2都可以同时评估_context.Users.Where(…),然后才能设置WelcomeLetterSent = true并调用SaveChanges.在这种情况下,可能会向每个用户发送两封欢迎电子邮件,而不是一封.

并发可能是一个阴险的事情.在here有一个关于使用Entity Framework管理并发性的入门知识,但这只是冰山一角.

你的问题的答案?这取决于你的应用程序做什么:)

On top of that,I ideally want to build some “extra” support applications that hook in to the same DB… and,I am just not sure how EF will handle multiple apps to the same DB….

如果您的应用程序可以容忍自己访问一个数据库的多个实例,那么通常不会让这些“支持应用程序”发挥得很好.并发性是来自一个应用程序的多个实例还是多个应用程序,每个应用程序各有一个实例.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读