2012-10-11 20 views
0

所以。ASP .NET MVC 4远程数据库上的键'attachdbfilename'的值无效

起初,我加入连接字符串到远程服务器MSSQL Server 2008 R2中,10.50.1600:

<add name="MySQLConnection" connectionString="server=xxx.x.xx.xx;initial catalog=xxxxx;user id=sa;password=xxxxxxxxxxx;"/> 

然后,我配置了在服务器端使用aspnet_regsql远程数据库在.NET 4.0中添加自定义成员(因为inbuild不想与WSAT合作)。

<membership 
    defaultProvider="SqlProvider" 
    userIsOnlineTimeWindow="20"> 
    <providers> 
    <clear /> 
    <add name="SqlProvider" 
     type="System.Web.Security.SqlMembershipProvider" 
     connectionStringName="MySQLConnection" 
     enablePasswordRetrieval="false" 
     enablePasswordReset="true" 
     requiresQuestionAndAnswer="true" 
     passwordFormat="Hashed" 
     applicationName="/" /> 
    </providers> 
</membership> 

所以,现在我可以配置我的WSAT应用程序,但不能在登录时输入或注册页面,出现错误:

System.ArgumentException: Invalid value for key 'attachdbfilename'. 

Line 32:      using (var context = new UsersContext()) 
Line 33:      { 
Line 34:       if (!context.Database.Exists()) 
Line 35:       { 
Line 36:        // Create the SimpleMembership database without Entity Framework migration schema 

解决:

的问题这里:

public class UsersContext : DbContext 
{ 
    public UsersContext() 
     : base("DefaultConnection") 
    { 
    } 

    public DbSet<UserProfile> UserProfiles { get; set; } 
} 

我不需要自定义会员等.-只是为我的连接字符串设置当前的DefaultConnection名称,或者在这里更改为MySQLConnectionString。

回答

0

attachdbfilename对远程数据库无效。它表示您的连接名称为MySQLConnection,但在您的成员资格中,您指定了一个“dbSkazkiEntities”连接字符串。

是否有多个连接字符串?

编辑:

哇。好的,你有很多事情搞砸了。首先,你正在使用SimpleMembership。 SimpleMembership不使用Web.Config中配置的成员资格提供程序。它绝对没有。

其次,SimpleMembership被配置在过滤器\ InitializeSimpleMembershipAttribute.cs文件中,有这样一行:

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

第一个参数告诉SimpleMembership要使用的连接字符串,它被称为DefaultConnection。 DefaultConnection是machine.config中定义的回退连接,并且此连接默认使用了一个localdb,它具有attachdbfilename属性。

您需要将上面的行更改为您的MySQLConnection,而不是您创建的MembershipProvider。

接下来是UsersContext也使用默认连接字符串定义。改变这一点。

+0

在我的例子中,你在哪看到attachdbfilename?我不使用它,我使用MSSQL – user1612334

+0

我认为他的意思是MySQLConnection,因为它是我的,而不是MySQL。 – Gromer

+0

Yeap,我测试过 - 但是,我改回到MySQLConnection,它仍然不起作用 – user1612334

相关问题