无法连接到Roles.IsUserInRole上的SQL Server数据库异常(" Admin")

时间:2016-01-29 07:21:08

标签: sql-server asp.net-mvc entity-framework asp.net-membership membership-provider

我得到了

  

无法连接到SQL Server数据库

行上的例外

isAdmin = Roles.IsUserInRole("Admin")

详细的内部异常是:

  

发生与网络相关或特定于实例的错误   建立与SQL Server的连接。找不到服务器或   无法访问。验证实例名称是否正确   SQL Server配置为允许远程连接。

我正在使用Asp.net MVC身份。我正在使用EF和数据第一种方法。我的web.config连接字符串是:

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Inspinia_MVC5_SeedProject-20141215093835.mdf;Initial Catalog=aspnet-Inspinia_MVC5_SeedProject-20141215093835;Integrated Security=True"  
         providerName="System.Data.SqlClient" />
    <add name="Entities" 
         connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDb)\v11.0;attachdbfilename=|DataDirectory|\aspnet-Inspinia_MVC5_SeedProject-20141215093835.mdf;initial catalog=aspnet-Inspinia_MVC5_SeedProject-20141215093835;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
    <add key="enableSimpleMembership" value="false" />
    <!-- others -->
</appSettings>

为了初始化简单成员资格,我在Global.asax.cs

中编写了以下课程
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
    }

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            try
            {
                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "Id", "UserName", autoCreateTables: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Something is wrong", ex);
            }
        }
    }
}

0 个答案:

没有答案