2015-05-28 43 views
0

我遇到问题。我创建了一个项目asp.net mvc 5.我将所有库owin和其他库文件迁移到类库中。 而我使用这个类库创建用户,登录和其他。 但是现在,我发现在app_data中创建了一个mdf,并且在创建用户或其他用户时,数据库没有更改,也没有创建。如何在ASP.Net标识中设置连接字符串

我认为所有的信息都在mdf文件中,因为如果重新启动应用程序,登录工作。 我在这篇文章上尝试了很多解决方案: Cannot attach the file *.mdf as database(在web.config中添加/删除attachfile,停止/删除sqllocaldb),但没有任何工作。

我的web.config是主要的应用程序(不库班)是这样的:

<configSections> 
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
</configSections> 
<connectionStrings> 
<add name="DefaultConnection" connectionString="Data Source=(localdb)\v11.0;initial catalog=WebMVCDataBase;integrated security=True;pooling=False;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> 
</connectionStrings> 
<appSettings> 
<add key="webpages:Version" value="3.0.0.0" /> 
<add key="webpages:Enabled" value="false" /> 
<add key="ClientValidationEnabled" value="true" /> 
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
<add key="userconnection" value="Data Source=(localdb)\v11.0;initial catalog=WebMVCDataBase;integrated security=True;pooling=False;MultipleActiveResultSets=True" /> 
</appSettings> 

和我的应用程序的DbContext是这样的:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("userconnection", throwIfV1Schema: false) 
    { 
     //Database.SetInitializer<ApplicationDbContext>(new CreateDatabaseIfNotExists<ApplicationDbContext>()); I try this but not work 
    } 


    public static ApplicationDbContext Create() 
    { 

     return new ApplicationDbContext(); 
    } 
} 

感谢您的帮助

回答

1

身份读取的ConnectionStrings标签连接字符串值。换句话说,它不会读取用户连接这是在应用程序设置里面。

因此,您需要将您的连接字符串名称重命名为DefaultConnection。这两个连接字符串在您的方案中是相同的。

enter image description here

+0

非常感谢,我搜索了很多帖子,但没有找到解决方案:)谢谢:) – Zoners

0

显然,当您第一次创建解决方案/项目数据库时,它将在您的项目的父目录中创建。之后当您构建/调试您的解决方案时,它将在Bin/Debug目录中添加数据库。调试时对数据所做的任何更改均在Debug数据库中进行。但是,每次在VS中运行应用程序时,都会从父目录中提取数据,该目录从未收到您在调试期间所做的更改,因为这些更改实际上是在Debug数据库中进行的。

•In database explorer 
    •Right click on the database 
    •Select modify connection 
    •Advanced options 
    •Search for the property AttachDbFileName 
    •Change it to the DB from debug folder c:...\bin\debug\DB.mdf 
    •Once you point your database to the Debug directory, in VS Server Explorer, you can then delete the database in the solution explorer and all updates will transpire in the Debug database. 

对于您的错误,请做到这一点

1.From Package Manager Console run: 

sqllocaldb.exe stop v11.0 

sqllocaldb.exe delete v11.0 


2.Run your project 

3.Register a user 

请检查该link

+0

那说:因为该数据库与其它应用程序连接不能附加MDF数据库。因为当我尝试这个命令时:“sqllocaldb.exe stop v11.0 sqllocaldb.exe delete v11.0”。我用这个命令删除所有数据库:s – Zoners

+0

请检查我的更新答案。尝试给数据库一个不同的名称或检查步骤。 – BSG

+0

当我使用此命令后,运行我的项目后,注册工作,但不是数据库创建....如果停止应用程序并运行项目,新用户工作。 – Zoners

相关问题