ASP.NET MVC 5与实体框架的并发性 [英] Concurrency in ASP .NET MVC 5 with Entity Framework

查看:127
本文介绍了ASP.NET MVC 5与实体框架的并发性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架在ASP .NET MVC 5中使用简单的CMS。
我有一些有关MVC应用程序中并发的问题。



首先 - 我的应用程序的公共部分(没有任何身份验证的用户部分):




  • 所有数据(帖子,类别信息,标签,文档)都存储在DB中(使用实体)

    / li>
  • 在公共部分的控制器中,只有读取或写入数据,而不是删除或编辑




所以我的第一个问题 - 有必要有一些机制来避免公共部分的并发危险?当多个用户同时浏览我的网站时,一切正常吗?



然后我的管理部分(需要验证):




  • 可以有多个具有多个角色的用户(但实际上只有少数注册用户)

  • 用户可以创建/编辑/删除数据中的数据



我知道一些安全机制是安全系统所必需的,但可以帮助我如何做到这一点?



我还在每种方法中使用DbContext的模式,其中需要DB:



using(var db = new CmsContext())
{
//其他内容
}



而不是在所有方法中使用的一个类变量db。这是对的吗?



非常感谢您的答案!

解决方案


是否需要有一些机制来避免公共部分中的并发危险
?多个用户将
同时浏览我的网站时,一切正常吗?


据我所知,并发性与两个人同时编辑相同的记录有关。考虑到公共部分没有任何写作方法,避免并发机制是不必要的,也不可能。


我知道一些安全机制是有必要的安全系统,
,但你能帮助我如何做到这一点吗?


你可以写你的在其他类中查询,如存储库和/或服务。在google上搜索仓库和服务实体框架,我发现这个链接 http://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing ,这听起来很有趣。


我也在每个方法中使用DbContext的这个模式,其中DB是
必需的:

  using(var db = new CmsContext()){// other stuff here} 

而不是在所有方法中使用的一个类变量db。这是
对吗?


这是一个难题。这几天真是太错了。有很多人,有几个不同的意见。在我看来,你的例子不是最好的做法(但并不意味着它是错误的)。当使用存储库类时,通常只有一个DbContext实例。看看这个线程 c#实体框架:正确使用您的存储库类中的DBContext类



希望这有帮助!


I´m working on simple CMS in ASP .NET MVC 5 with Entity Framework. I have a few questions about concurrency in MVC´s applications.

First of all - public section of my app (section for users without any authentification):

  • All data (posts, category informations, tags, documents) are stored in DB (using Entity)

  • In controllers for public section, there is only reading or writing data to DB, not deleting or editing

So my first question - is it necessary to have some mechanisms to avoid concurrency dangers in public section? Will everything be correct when multiple users will be browsing my website at the same time?

And then my admin section (authentification needed):

  • There can be multiple users with multiple roles (but in real there will be only few registered users)
  • Users can create/edit/delete data in DB

I know that some security mechanism are necessary to have safe system, but can you help me how to do this?

I´m also using this pattern for DbContext in each method, where DB is necessary:

using (var db = new CmsContext()) { // other stuff here }

Instead of one class variable db that is used in all methods. Is this right?

Many thanks for answers!

解决方案

is it necessary to have some mechanisms to avoid concurrency dangers in public section? Will everything be correct when multiple users will be browsing my website at the same time?

As far as I can see, concurrency is related with two people editing the same record at the same time. Considering that the public section doesn't have any writing method, avoid concurrency mechanism is not necessary, neither is possible.

I know that some security mechanism are necessary to have safe system, but can you help me how to do this?

You could write your queries in others classes, like repositories and/or services. Searching for "repository and services entity framework" at google, I found this link http://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing, which sounds very interesting.

I´m also using this pattern for DbContext in each method, where DB is necessary:

using (var db = new CmsContext()) { // other stuff here }

instead of one class variable db that is used in all methods. Is this right?

That's a hard question. It is too hard to say that something is right or wrong these days. There are a lot of people, with several different opinions. In my opinion, your example is not the best way to do (but it doesn't mean it's incorrect). When using a repository class, we usually have only one instance of the DbContext. Take a look at this thread c# entity framework: correct use of DBContext class inside your repository class

Hope this helps!

这篇关于ASP.NET MVC 5与实体框架的并发性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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