我正在使用带有 SQL Server Express 2012 的 Visual Studio 2012。我使用“Internet 应用程序”模板创建了一个 MVC 4 Web 应用程序。
我想在我的 SQL Server 2012 Express 实例上使用现有数据库(而不是模板的 MDF)。我已修改我的 web.config 以指向我的本地 SQL express 实例,并删除了模板生成的 MDF。我的 Web.Config 有:
<add name="DefaultConnection"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Administration;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
股票会员代码有一个初始化数据库的调用:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
一旦应用程序启动,这些表就会在管理数据库中创建,正如我所期望的那样。我可以创建用户并在管理中填充它们。我可以通过代码填充角色表并将角色添加到用户的配置文件并验证它是否已完成。例如:
Roles.GetRolesForUser("kristian"); // = string[] { "Administrator" }
但是,如果我尝试使用基于角色的授权,例如:
[Authorize(Roles = "Administrator")]
我得到以下异常:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
它给出了一个很长的讨厌的堆栈跟踪,让我觉得它仍在尝试创建一个 MDF 文件:
[SqlException (0x80131904):由于启动用户实例的进程失败,无法生成 SQL Server 的用户实例。连接将被关闭。] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常, Boolean breakConnection, Action 1 wrapCloseInAction) +5295167
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +69
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +30
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +317
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +889
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5309659
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +5311874 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory , TaskCompletionSource 1 retry, DbConnectionOptions userOptions) +143
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 次重试) +83 System.Data.SqlClient.SqlConnection.Open() +96 System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +76
[HttpException (0x80004005): 无法连接到 SQL Server 数据库。] System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +131 System.Web.Management.SqlServices。 SetupApplicationServices(字符串服务器,字符串用户,字符串密码,布尔信任,字符串连接字符串,字符串数据库,字符串 dbFileName,SqlFeatures 功能,布尔安装)+89 System.Web.Management.SqlServices.Install(字符串数据库,字符串 dbFileName,字符串连接字符串) +27 System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(字符串 fullFileName,字符串 dataDir,字符串 connectionString)+386
这个快乐捆绑底部的行让我认为它与现在删除的 MDF('System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile')有关。我在这方面是正确的吗?还是模板实现的授权不完整?
如果其他人已经处理过这个问题或者知道我的属性使用失败的原因,我将非常感谢您的帮助。