当使用带有EF 5代码优先的ASP.NET默认成员身份时找不到连接字符串“ DefaultConnection”

问题描述 投票:1回答:2

我已经使用EF 5 Code First创建了我的应用程序域模型。然后,我尝试通过运行aspnet_reqsql.exe实用程序以在数据库中生成成员资格表,从而将其与ASP.NET默认成员资格提供程序挂钩。

这似乎很好用,因为当我在SQL Management Studio中检查数据库时,所有成员资格表以及我的Code First表都在那里。

现在,我运行我的应用程序,一切看起来都很好,它已加载并且我可以浏览页面。但是,一旦我浏览了需要会员资格的内容(例如登录页面),应用程序就会中断。我收到带有以下消息的死亡黄屏:

Server Error in '/' Application.

Connection string "DefaultConnection" was not found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Connection string "DefaultConnection" was not found.

Source File: c:\Users\XXX\Documents\Visual Studio 2012\Projects\YourApp\YourApp\Filters\InitializeSimpleMembershipAttribute.cs    Line: 41

[用户有一个this thread面临类似的问题,但是我已经实现了他的解决方案,这一事实是我必须注释掉web.config中已经存在的连接字符串。我已经做到了,但仍然没有成功。

我在做什么错?

ef-code-first asp.net-membership entity-framework-5
2个回答
2
投票

在调用InitializeSimpleMembershipAttribute的位置,如果仍然将"DefaultConnection"作为第一个参数,则需要将其更改为您要使用的连接的名称。


0
投票

感谢理查德,我设法将所有问题整理了出来。这是我所做的:

我打开了我的AccountController,在类的顶部,您会看到此信息(如果使用ASP.NET MVC4):

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
    // all code for controller here.
}

然后我在[InitializeSimpleMembership]上单击鼠标右键,然后单击Go To Definition

一旦出现,如果滚动到底部,您将看到类似这样的内容:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

DefaultConnection属性更改为您已命名为EF Code First Context的名称,您就很高兴了。

感谢理查德。

© www.soinside.com 2019 - 2024. All rights reserved.