Skip to content

Instantly share code, notes, and snippets.

@carloscds
Created February 28, 2020 01:13
Show Gist options
  • Save carloscds/47565a0bd42ead503848fdf2ad8e81d4 to your computer and use it in GitHub Desktop.
Save carloscds/47565a0bd42ead503848fdf2ad8e81d4 to your computer and use it in GitHub Desktop.
using InjecaoDependenciaDiretaEF.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace InjecaoDependenciaDiretaEF.Data
{
public class AplicacaoContext : DbContext
{
public AplicacaoContext(DbContextOptions<AplicacaoContext> options) : base(options)
{
SeedData();
}
public DbSet<Cliente> Cliente { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Cliente>()
.Property(p => p.Id)
.IsRequired();
builder.Entity<Cliente>()
.Property(p => p.Nome)
.HasMaxLength(100);
base.OnModelCreating(builder);
}
private void SeedData()
{
Cliente.Add(new Models.Cliente { Nome = "Carlos", LimiteCredito = 1000 });
Cliente.Add(new Models.Cliente { Nome = "Maria", LimiteCredito = 5000 });
Cliente.Add(new Models.Cliente { Nome = "Jose", LimiteCredito = 500 });
Cliente.Add(new Models.Cliente { Nome = "Joana", LimiteCredito = 600 });
SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment