1

I want to set my existing database TestDB into my application so that Identity will create their tables inside this database.

But I am getting error:

Cannot attach the file 'D:\TestApp\TestApp\App_Data\TestDB.mdf' as database 'TestDB'.

I have done couple of things which are

  1. I removed defaultconnection to TestDBConnection in web.config

    <add name="TestDBConnection"
    connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TestDB.mdf;Initial Catalog=TestDB;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    
  2. I changed in IdentityModels.cs

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
       public ApplicationDbContext()
            : base("TestDBConnection", throwIfV1Schema: false)
       { }
    
  3. In package Manager Console,

    PM>enable-migrations
    
  4. In Configuration.cs,

    internal sealed class Configuration : 
    DbMigrationsConfiguration<TestApp.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }
    

Please guide me where am I going wrong. I am looking for your suggestions.

GSerg
  • 76,472
  • 17
  • 159
  • 346
Raj
  • 555
  • 1
  • 12
  • 31
  • 1
    It would appear your problem has nothing to do with [making ASP.NET Identity use an existing database](https://stackoverflow.com/q/25651342/11683). Verify that you can [attach that database file](https://stackoverflow.com/q/17012839/11683) in principle. – GSerg Dec 27 '18 at 12:28
  • @GSerg, I just changed connection string to `` and it worked for me now. Thank you – Raj Dec 27 '18 at 12:31

0 Answers0