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?– Ricardo Pontual
Yes, it is attached and online! My version is the 2014
– Márcio Sebastião
And on your show, how is connection string?
– Ricardo Pontual
<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>
– Márcio Sebastião