Entity Framework (Populating tables)

Asked

Viewed 63 times

0

What is wrong with my project? When I try to give an Update-Database error message appears (The underlying provider failed on Open). When I remove the items I want to add, for example, highlighteVeicule, modelVeicule and versionVeicule works. Will I’m missing in the source, any help will be welcome.

using broker.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;

namespace broker.Contexto
{
    public class DBContext : DbContext
    {

        public DBContext()
        : base("CotacaoApp")
        {
            if (!marcaVeiculos.Any())
            {
                marcaVeiculos.Add(new MarcaVeiculo() { MarcaId = 1, Marca = "CHERVROLET" });
                marcaVeiculos.Add(new MarcaVeiculo() { MarcaId = 2, Marca = "CITROEN" });
                marcaVeiculos.Add(new MarcaVeiculo() { MarcaId = 3, Marca = "FIAT" });
                marcaVeiculos.Add(new MarcaVeiculo() { MarcaId = 4, Marca = "FORD" });
                marcaVeiculos.Add(new MarcaVeiculo() { MarcaId = 5, Marca = "HONDA" });
                marcaVeiculos.Add(new MarcaVeiculo() { MarcaId = 6, Marca = "HYUNDAI" });
                SaveChanges();
            }
            if (!modeloVeiculos.Any())
            {
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 1, Modelo = "ASTRA", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 2, Modelo = "CAMARO", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 3, Modelo = "CAPTIVA", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 4, Modelo = "CELTA", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 5, Modelo = "CLASSIC", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 6, Modelo = "COBALT", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 7, Modelo = "CORSA", MarcaId = 1 });
                modeloVeiculos.Add(new ModeloVeiculo() { ModeloId = 8, Modelo = "CORVETE", MarcaId = 1 });
                SaveChanges();
            }
            if (!versaoVeiculos.Any())
            {
                versaoVeiculos.Add(new VersaoVeiculo() { VersaoId = 1, Versao = "CHEVROLET ASTRA 2.0", ModeloId = 1 });
                versaoVeiculos.Add(new VersaoVeiculo() { VersaoId = 2, Versao = "CHEVROLET ASTRA 2.0 8V 4P", ModeloId = 1 });
                SaveChanges();
            }

        }
        public DbSet<Cliente> Clientes { get; set; }
        public DbSet<MarcaVeiculo> marcaVeiculos { get; set; }
        public DbSet<ModeloVeiculo> modeloVeiculos { get; set; }
        public DbSet<VersaoVeiculo> versaoVeiculos { get; set; }
        //public DbSet<UserAccount> userAccounts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Properties()
                .Where(p => p.Name == p.ReflectedType.Name + "Id")
                .Configure(p => p.IsKey());

            modelBuilder.Properties<string>()
                .Configure(p => p.HasColumnType("varchar"));

            modelBuilder.Properties<string>()
               .Configure(p => p.HasMaxLength(100));

        }
    }
}
  • Shouldn’t create a Seed

  • So when I try Enable-Migrations I have the same error. If I delete and create only the empty tables it works. Is there a problem I create the tables and manually popular right in the bank??? The problem with that is that when I delete the bank and make a new Update-Database I lose everything I put in the bank manually. I think populating directly in Dbcontext this will not occur anymore.

  • Is there any problem I create the tables fazias and popular manually direct in the bank Has not.

  • I got it. I put it in the file Configuration.Cs that it generates when I enable Migrations. It was clear when you spoke Seed. My Seed is for it. Vlw my friend.

1 answer

0

I put it that way. It worked out nice:

 using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<broker.Contexto.DBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(broker.Contexto.DBContext 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.

        var marca = new List<MarcaVeiculo>
        {
        new MarcaVeiculo{MarcaId=1, Marca ="CHEVROLET"},
        new MarcaVeiculo{MarcaId=2, Marca ="CITROEN"}
        };

        marca.ForEach(s => context.marcaVeiculos.Add(s));
        context.SaveChanges();

        var modelo = new List<ModeloVeiculo>
        {
        new ModeloVeiculo{ModeloId=1, Modelo ="ASTRA", MarcaId=1},
        new ModeloVeiculo{ModeloId=2, Modelo ="AGILE", MarcaId=1}
        };

        modelo.ForEach(s => context.modeloVeiculos.Add(s));
        context.SaveChanges();

        var versao = new List<VersaoVeiculo>
        {
        new VersaoVeiculo{VersaoId=1, Versao ="CHEVROLET ASTRA 2.0", ModeloId=1},
        new VersaoVeiculo{VersaoId=2, Versao ="CHEVROLET ASTRA 2.0 8V 4P", ModeloId=1}
        };

        versao.ForEach(s => context.versaoVeiculos.Add(s));
        context.SaveChanges();
    }
}

}

Browser other questions tagged

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