EntityFramework - new table was not created

2

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>
    
asked by anonymous 18.08.2017 / 15:07

2 answers

1

I discovered the problem, connectionString was only in App.config of DataAccess, I put it in the Console and solved it. Again it was a problem with connectionString.

    
19.08.2017 / 22:41
1

It is often necessary to delete the migrations and recompile the project and re-run Update-Database .

Sometimes it is also necessary that user Update-Database -Force

To see what was done use -Verbose , Update-Database -Force -Verbose

Remember to change in your class Configuration o AutomaticMigrationsEnabled = true; leave as true .

See how to use Entity Framework Migrations .

    
18.08.2017 / 16:06