2

I have a problem. I create a project asp.net mvc 5. I migrate all library owin and other in class library. And I use this class library to create user,loggin and other. But now, I see that a mdf is create in app_data and the problem when I create a user or other, the database is not changes and not create.

I think all information are in the mdf file, because if relaunch the application the loggin work. I try many solution on this post: Cannot attach the file *.mdf as database (add/remove attachfile in web.config, stop/delete sqllocaldb) but nothing works.

My web.config is main application(not in library class) is like this:

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

and my Application dbcontext is like this:

  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();
    }
}

thanks for your help

Community
  • 1
  • 1
Zoners
  • 123
  • 1
  • 6
  • 11

2 Answers2

3

Identity reads connection string value from connectionStrings tag. In other words, it will not read userconnection which is inside appSettings.

So you need to rename your connection string name to DefaultConnection. Those two connection strings are same in your scenario.

enter image description here

Win
  • 61,100
  • 13
  • 102
  • 181
0

Apparently when you first create your solution/project database it is created in the parent directory of your project. Afterward when you build/debug your solution it adds the database in the Bin/Debug directory. Any changes you make to your data while debugging are made in the Debug database. However, when you run your application each time in VS it pulls the data from the parent directory which never received the changes that you made while debugging because the changes were actually made in the Debug database.

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

For your error please do this

1.From Package Manager Console run:

sqllocaldb.exe stop v11.0

sqllocaldb.exe delete v11.0


2.Run your project

3.Register a user

Please check this link

Community
  • 1
  • 1
BSG
  • 2,084
  • 14
  • 19
  • That say : cannot attach mdf to database because the database is attach with other application. Because when I try this command:"sqllocaldb.exe stop v11.0 sqllocaldb.exe delete v11.0". I delete all database with this command :s – Zoners May 28 '15 at 14:11
  • Please check my updated answer. Try giving the database a different name or check the steps. – BSG May 28 '15 at 14:15
  • When I Use this command, after run my project, register work, but not database is create....if a stop the application and run project, the new user work. – Zoners May 28 '15 at 14:18
  • Try giving the database a different name. Sometimes you can run into problems with SQL Express when trying to create a database with the same name a second time. There is a way to fix this using SQL Server Management Studio but it's generally easier to just use a different database name. – BSG May 28 '15 at 14:22