Entity Framework - tables were not created

3

I'm following an Entity Framework workbook, I created the classes:

public class k19Context : DbContext
    {
        public k19Context() : base("k19Context")
        {
        }

        public DbSet<Turma> Turmas { get; set; }

        public DbSet<Aluno> Alunos { get; set; }

        public DbSet<Professor> Professores { get; set; }
    }

[Table("Turmas")]
    public class Turma
    {
        public int TurmaId { get; set; }

        public int Vagas { get; set; }

        public Professor Professor { get; set; }

        public ICollection<Aluno> Alunos { get; set; }

    }

[Table("Professores")]
    public class Professor
    {
        public int ProfessorID { get ; set ; }

        [Required]
        public string Nome { get; set ; }

        public Endereco Endereco { get ; set ; }
    }

[Table("Alunos")]
    public class Aluno
    {
        public int AlunoID { get; set ; }

        [Required]
        [MaxLength(30), MinLength(3)]
        public string Nome { get; set ; }

        public DateTime DataDeNascimento { get; set; }

        [NotMapped]
        public int Idade { get; set; }

        public Endereco Endereco { get ; set ; }
    }

[Table("EnderecoPorAnotacao")]
    public class Endereco
    {
        public int EnderecoID { get; set; }

        public string Logradouro { get; set ; }

        public int Numero { get; set ; }

        public string CEP { get ; set; }
    }

I executed the project, I'm using asp net, and only the database was created, the tables were not created. What happened? I'm using SqlServer.

I tried some commands: Enable-Migrations , Update-DataBase . Then I tried to pass data to the entity:

public class Teste
    {
        public void Metodo()
        {
            Aluno aluno = new Aluno();

            aluno.DataDeNascimento = DateTime.Now;
            aluno.Endereco = new Endereco();
            aluno.Endereco.Logradouro = "rua teste";
            aluno.Endereco.Numero = 100;
            aluno.Endereco.CEP = "123";
            aluno.Idade = 18;
            aluno.Nome = "fulano";

            k19Context contexto = new k19Context();
            contexto.Alunos.Add(aluno);
            contexto.SaveChanges();
        }
    }

But the bank is still without tables.

Here is the web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<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>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Configuration.cs:

internal sealed class Configuration : DbMigrationsConfiguration<EntityFrameworkTestes.k19Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(EntityFrameworkTestes.k19Context 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. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }

Anything else I need to add?

    
asked by anonymous 14.01.2017 / 12:14

2 answers

2

Your Web.config does not have a connection string :

<configuration>
  ...
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=.\SQLEXPRESS;Database=MeuSistema;Integrated Security=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  ...
</configuration>

This is valid for Microsoft SQL Server Express. Typically, projects in ASP.NET MVC come with a LocalDb database created for you in App_Data (the file is usually hidden in Solution Explorer).

    
14.01.2017 / 16:43
1

It's all right wherever you went, now you need to give the commands:

'add-migration nome_da_migration' 

and another

'update-database',

Then go to the SQL Server window Object Explorer clicks with the right mouse button on the Database and a Refresh. You will see the created database and its tables.

    
14.01.2017 / 16:10