(Localdb) Error after Update-Database command -> "Cannot attach the file ...."

Asked

Viewed 215 times

0

I am trying to use Sql Serve Localdb in an application with Entity Framework and Migrations, but when I give the Update-Database command, the following error occurs:

Cannot attach the file 'C: 9-Project Directorosmvs Testededb Testededb bin Debug Testedb.mdf' as database 'Testedb'.

I’ve tried to:

sqllocaldb.exe stop Mssqllocaldb

sqllocaldb.exe delete Mssqllocaldb

And did not resolve... :/

This is my Context:

using System.Data.Entity;
using TesteDeDB.Entidades;

namespace TesteDeDB.Contexto
{
    class ClasseContexto : DbContext
    {
        public ClasseContexto()
            : base("name=Model1")
        {
        }

        public DbSet<Cliente> clientes { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}

This is the Migrations Configuration class:

namespace TesteDeDB.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Contexto.ClasseContexto>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(Contexto.ClasseContexto context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.
        }
    }
}

And this is the entity I’m trying to create in the comic book:

namespace TesteDeDB.Entidades
{
    public class Cliente
    {
        public int clienteID { get; set; }
        public string nome { get; set; }
        public string email { get; set; }
    }
}

And this is my String Connection:

<connectionStrings>
    <add name="Model1" connectionString="data source=(LocalDb)\MSSQLLocalDb;attachdbfilename=|DataDirectory|\TesteDB.mdf;initial catalog=TesteDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
  • Plugged into the sql-server, using for example the Management Studio and checked if the database "Testedb" is tied and online?

  • Yes, it is attached and online! My version is the 2014

  • And on your show, how is connection string?

  • <connectionStrings>&#xA; <add name="Model1" connectionString="data source=(LocalDb)\MSSQLLocalDb;attachdbfilename=|DataDirectory|\TesteDB.mdf;initial catalog=TesteDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> </connectionStrings>

1 answer

2


The problem is in your connection string. This part:

attachdbfilename=|DataDirectory|\TesteDB.mdf tell the bank that he must Set the file "Testedb.mdf" that is in "Datadirectory".

If the bank already exists and is tied, you need to remove that part of the connection string:

<add name="Model1" connectionString="data source=(LocalDb)\MSSQLLocalDb;initial catalog=TesteDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

Also, you may have to put the sql version if it doesn’t work like this: Server=(localdb)\\v12.0;. This is because you are using the 2014 version, for the 2012 version is "V11.0", and so on: SQL Server instances

  • It worked out! As the bank already exists just change the string Connection just like you said :-D thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.