Estou seguindo uma apostila de Entity Framework, criei as 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; }
}
Executei o projeto, estou usando o asp net, e somente o banco foi criado, as tabelas não foram criadas. O que aconteceu? Estou usando o SqlServer.
Tentei alguns comandos: Enable-Migrations
, Update-DataBase
. Depois tentei passar dados à entidade:
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();
}
}
Porém o banco continua sem tabelas.
Aqui está o 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" }
// );
//
}
}
Algo mais que preciso acrescentar?
AutomaticMigrationsEnabled = false;
deveria serAutomaticMigrationsEnabled = true;
para funcionarEnable-Migrations, Update-DataBase