3
I am following an Entity Framework booklet, 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 ran the project, I am using Asp net, and only the database was created, the tables were not created. What happened? I am 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 still has no 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" }
// );
//
}
}
Something else I need to add?
Your connectionString (k19Context) failed to put in your question? and there are also things missing ...
– novic
I’ll put, what else is missing? Maybe I haven’t done what I’m missing.
– HeyJoe
Put on the connectionStrings I’ll check
– novic
Actually I did not make the connectionString. I already edited the post. The database was created, only the tables that.
– HeyJoe
Your Migration is not enabled:
AutomaticMigrationsEnabled = false;
should beAutomaticMigrationsEnabled = true;
to workEnable-Migrations, Update-DataBase
– novic
In your configuration file is not present the connection configuration so also will not do anything!
– novic
I’ve already made these Enable-Migrations, Update-Database commands. "connection configuration" you’re talking about connectionString?
– HeyJoe
Let’s go continue this discussion in chat.
– novic