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.

2
  • 1
    It would appear your problem has nothing to do with making ASP.NET Identity use an existing database. Verify that you can attach that database file in principle. Commented Dec 27, 2018 at 12:28
  • @GSerg, I just changed connection string to <add name="TestDBConnection" connectionString="Data Source=LAPTOP-E06VT3CD\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True" providerName="System.Data.SqlClient" /> and it worked for me now. Thank you Commented Dec 27, 2018 at 12:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.