上传到托管服务器后,实体框架连接错误

时间:2015-07-10 05:50:21

标签: c# asp.net entity-framework ef-code-first

我使用asp.net和entityframework为数据库构建了Application 这是我的上下文类的连接字符串

<add name="EStringspbcontext" connectionString="Connection string: Data Source=localhost;
Initial Catalog=EStringspb;Integrated Security=False;User ID=sqladmin;Connect Timeout=15;
Encrypt=False;Packet Size=4096;MultipleActiveResultSets=True;
App=EntityFramework;Persist Security Info = True" providerName="System.Data.SqlClient"/>

当我尝试登录时会抛出错误

它适用于本地计算机,但是当我们在托管服务器上传时,它会抛出以下错误

  

&#34;此操作需要连接到&#39; master&#39;数据库。无法   建立与主人的联系&#39;数据库因为原来的   数据库连接已打开,凭据已被删除   来自连接字符串。提供未打开的连接。&#34;

这是我的登录代码

string emailid ="johnddoe@gmail.com";
UserEntity user = new UserEntity();
                using (EStringspbcontext db = new EStringspbcontext())
                {                        
                    var objuser = (from obj in db.Users where obj.EmailId == emailid  select obj).SingleOrDefault();
                    user = (UserEntity)objuser;
                    db.Dispose();
                }
                    if (user.Password == txtadminPassword.Value)
                    {
                        //if (user.OnlineStatus == false)
                        //{
                        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
                        {
                            using (SqlCommand cmd = new SqlCommand("update Usersdetails set OnlineStatus = '" + true + "' where EmailId ='" + user.EmailId.ToString() + "'", con))
                            {
                                con.Open();
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        } 
  }          

2 个答案:

答案 0 :(得分:1)

我认为您必须根据您的托管服务器更新连接字符串。就像您必须更改服务器一样。

<add name="EStringspbcontext" connectionString="Server=ipofserver;initial catalog=EStringspb;Trusted_Connection = True ;MultipleActiveResultSets=True;App=EntityFramework;" providerName="System.Data.SqlClient"/>

答案 1 :(得分:1)

我忘了在将Password = abc添加到我工作的连接字符串

后,在连接字符串中添加密码

这是连接字符串

<connectionStrings>
<add name="democontext" connectionString="Server=server_name;Initial Catalog=database_name;Integrated Security=False;User ID=user_id;Password=password;Connect Timeout=15;Encrypt=False;Packet Size=4096;MultipleActiveResultSets=True;App=EntityFramework;Persist Security Info = True;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
</connectionStrings>