2
Following this tutorial the tables were created all right.
Then I took a test, created a new entity, did the update-database
and no creating the table in SQL Server 2012. I made some changes to the entity, added Migrations, I tried to do an update again, but it does not create the table at all.
namespace Domain.entities
{
public class teste
{
public teste() {
teste t = new teste();
t.idade = 15;
}
public int testeId { get; set; }
public int idade { get; set; }
public string nome { get; set; }
}
}
namespace DataAccess.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class teste : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.teste",
c => new
{
testeId = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.testeId);
}
public override void Down()
{
DropTable("dbo.teste");
}
}
}
namespace DataAccess.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class teste4 : DbMigration
{
public override void Up()
{
AddColumn("dbo.teste", "nome", c => c.String(maxLength: 100, unicode: false));
}
public override void Down()
{
DropColumn("dbo.teste", "nome");
}
}
}
namespace DataAccess
{
class DataContext : DbContext
{
public DataContext(): base("DataContext")
{
}
public DbSet<Aluno> Alunos { get; set; }
public DbSet<Turma> Turmas { get; set; }
public DbSet<Curso> Cursos { get; set; }
public DbSet<Professor> Professores { get; set; }
public DbSet<Usuario> Usuarios { get; set; }
public DbSet<teste> Testes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Properties()
.Where(p => p.Name == p.ReflectedType.Name + "Id")
.Configure(p => p.IsKey());
modelBuilder.Properties<string>()
.Configure(p => p.HasColumnType("varchar"));
modelBuilder.Properties<string>()
.Configure(p => p.HasMaxLength(100));
}
}
}
Why not create the "test table"?
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="DataContext"
connectionString="Server=DW; Database=EFEscola; uid=sa; password=123456;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Are you looking in the right bank? Already checked the string Connection?
– Jéf Bueno
Yes, because I did not do any database modification, I finished the tutorial and I already did this test.
– HeyJoe
Make a select in the table
__MigrationHistory
and see which lastMigrationId
– Jéf Bueno
It has only one: 2017081156480_AutomaticMigration
– HeyJoe
Well, then that means you you’re not using Migrations in your project. That is, the update is always running only
Update-Database
. No use creating Migration because the EF will not try to use it. Delete the Migration file and run aUpdate-Database
on the console.– Jéf Bueno
I deleted some classes of Migration there, there was only one "Configuration.Cs". I did the update-database and returned it here: Specify the '-Verbose' flag to view the SQL statements being Applied to the target database. No pending Explicit Migrations. Running Seed method.
– HeyJoe
And the bank was changed? By the way, you are updating the SSMS to check the modifications, right?
– Jéf Bueno
You didn’t create the table. Yes, I update the bank to see. A question, when I enable {Automaticmigrationsenabled} can no longer create Migrations just use the update-database?
– HeyJoe
That’s right. Apparently you started one way and went the other way. If I were you, I would test with another bank.
– Jéf Bueno
created another database, deletes the Migrations folder, enabled another one, but now no table has been created. Is it not using SQL Server? Strange, because I didn’t modify the connection string that had worked.
– HeyJoe