Following this tutorial tables all correct.
Then I went to do a test, I created a new entity, I made update-database
and nothing to create the table in SQL Server 2012. I made some changes in the entity, added migrations , 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>