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
– novic
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.
– edilcabral
Is there any problem I create the tables fazias and popular manually direct in the bank Has not.
– novic
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.
– edilcabral