Entity Framework - tables not created

Asked

Viewed 96 times

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

  • I’ll put, what else is missing? Maybe I haven’t done what I’m missing.

  • Put on the connectionStrings I’ll check

  • Actually I did not make the connectionString. I already edited the post. The database was created, only the tables that.

  • Your Migration is not enabled: AutomaticMigrationsEnabled = false; should be AutomaticMigrationsEnabled = true; to work Enable-Migrations, Update-DataBase

  • In your configuration file is not present the connection configuration so also will not do anything!

  • I’ve already made these Enable-Migrations, Update-Database commands. "connection configuration" you’re talking about connectionString?

Show 3 more comments

2 answers

2


Your Web.config does not possess 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, ASP.NET MVC projects come with a Localdb database created for you at App_Data (the file is usually hidden in Solution Explorer).

1

It’s all right up to where you did, now you must give the commands:

'add-migration nome_da_migration' 

and another

'update-database',

then go to the SQL Server Object Explorer window and click with the right mouse button in the Database and a Refresh. The created database and its tables will appear.

Browser other questions tagged

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