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?