使用与ADO.NET实体框架的静态数据访问方法 [英] Using static data access methods with the ADO.NET Entity Framework

查看:113
本文介绍了使用与ADO.NET实体框架的静态数据访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我现在用的是ADO.NET实体框架的第一次和静态code分析建议我改变下面的方法静态的,如下。

我的问题很简单,这是线程安全的?

 公共静态无效InsertUserDetails(UserAccount userAccount)
        {
            使用(KnowledgeShareEntities实体=新KnowledgeShareEntities())
            {
                用户用户=新用户();
                user.usr_firstname = userAccount.FirstName;
                user.usr_surname = userAccount.LastName;
                user.usr_email = userAccount.Contact.Email;
                user.usr_logon_name = userAccount.SAMUserAccountName.ToUpper();
                user.usr_last_login_datetime = DateTime.Now;
                entities.AddToUsers(用户);
                entities.SaveChanges();
            }
        }
 

解决方案

由于您只使用局部变量的方法是线程安全的。这里涉及到那么一切都将在线程局部栈没有静态变量,可能会出现没有竞争条件。

Hi I am using the ADO.NET entity framework for the first time and the staticcode analysis is suggesting I change the following method to a static one as below.

My question is simple, is this thread safe?

public static void InsertUserDetails(UserAccount userAccount)
        {
            using (KnowledgeShareEntities entities = new KnowledgeShareEntities())
            {
                Users user = new Users();
                user.usr_firstname = userAccount.FirstName;
                user.usr_surname = userAccount.LastName;
                user.usr_email = userAccount.Contact.Email;
                user.usr_logon_name = userAccount.SAMUserAccountName.ToUpper();
                user.usr_last_login_datetime = DateTime.Now;
                entities.AddToUsers(user);
                entities.SaveChanges();
            }
        }

解决方案

As you are using only local variables the method is thread safe. There are no static variables involved so everything will be on the thread local stack and no race conditions could occur.

这篇关于使用与ADO.NET实体框架的静态数据访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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