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

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

问题描述

I'm在ASP .NET MVC 5与实体框架上简单的CMS工作。
我有并发的几个问题中的应用MVC's

首先 - 我的应用程序的公共部分(部分用户无需任何认证方式):


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


  • 在控制器的公共部分,有只读取或写入数据到数据库,不能删除或修改


所以,我的第一个问题 - 是否需要有一些机制,以避免在公共部分并发的危险?将一切是正确的,当多个用户将在同一时间浏览我的网站?

然后我的管理部分(鉴别需要):


  • 可以有多个用户使用多个角色(但在现实会有只有少数注册用户)

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

我知道一些安全机制都需要有安全的系统,但你可以帮助我如何做到这一点?

I'm也是用这个模式来的DbContext每个方法,其中,DB是必要的:

使用(VAR DB =新CmsContext())
{
//这里其他的东西
}

而不是在所有的方法使用了一个类变量分贝。这是正确的?

答案非常感谢!


解决方案

  

是否需要有一些机制来避免并发危险
  在公共部门?一切都将是正确的,当有多个用户
  可以浏览我的网站在同一时间?


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


  

我知道一些安全机制都需要有安全的系统,
  但你可以帮助我如何做到这一点?


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


  

I'm也是用这个模式来的DbContext每个方法,其中,DB是
  必要的:

 使用(VAR DB =新CmsContext()){//这里其他的东西}


  
  

中而不是在所有的方法使用了一个类变量分贝。这是
  对吧?


这是一个很难回答的问题。实在是太很难说什么是对还是错,这些天。有很多人,有几个不同的意见。在我看来,你的榜样是不是做的最好的方式(但并不意味着它是不正确的)。当使用存储库类,我们通常有的DbContext只有一个实例。看看这个线程<一个href=\"http://stackoverflow.com/questions/33041113/c-sharp-entity-framework-correct-use-of-dbcontext-class-inside-your-repository/33193061#33193061\">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天全站免登陆