当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - Unable to connect to SQL Server database exception on Roles.IsUserInRole("Admin")

问题描述:

I get

Unable to connect to SQL Server database

exception on line

isAdmin = Roles.IsUserInRole("Admin")

detailed inner exception is:

A network-related or instance-specific error occurred while

establishing a connection to SQL Server. The server was not found or

was not accessible. Verify that the instance name is correct and that

SQL Server is configured to allow remote connections.

I am using Asp.net MVC identity. And I am using EF with data first approach. My web.config connection string is:

<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>

To initiallize simple membership, I wrote following class in 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);

}

}

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: