Entityframework - new table not created

Asked

Viewed 751 times

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?

  • Yes, because I did not do any database modification, I finished the tutorial and I already did this test.

  • Make a select in the table __MigrationHistory and see which last MigrationId

  • It has only one: 2017081156480_AutomaticMigration

  • 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 a Update-Database on the console.

  • 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.

  • And the bank was changed? By the way, you are updating the SSMS to check the modifications, right?

  • 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?

  • That’s right. Apparently you started one way and went the other way. If I were you, I would test with another bank.

  • 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.

Show 5 more comments

2 answers

1

It is often necessary to delete the files from Migrations and recompile the project and run the Update-Database.

Sometimes it is also necessary to use the Update-Database -Force

To see what was executed use the -Verbose, Update-Database -Force -Verbose

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

Behold how to use Entity Framework Migrations.

  • See: 'PM> Update-Database -Force -Verbose Using Startup project 'Console'. Using Nuget project 'Dataaccess'. Specify the '-Verbose' flag to view the SQL statements being Applied to the target database. Target database is: 'Datacontext' (Datasource: (localdb) v11.0, Provider: System.Data.Sqlclient, Origin: Convention). No pending Explicit Migrations. Running Seed method.'

  • Sorry, but how do I put here as a code snippet, I refer to formatting. I will post App.config in my post.

  • Click on the help beside use ` to open and close ... you are running it in the Console project. is in it even your EF ?

  • I am running the commands in Dataaccess where Datacontext is. It would not be a communication problem with SQL Server?

  • What would be DataSource and (localdb)\v11.0 ?

1


I discovered the problem, the connectionString was just in the App.config Dataaccess, I put in the Console and solved. Again it was a problem with connectionString.

Browser other questions tagged

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