Last active
October 20, 2021 00:56
-
-
Save Marceloromeugoncalves/e739d5fa1fbbab0412d4b8e00b6cb23a to your computer and use it in GitHub Desktop.
Exemplo de utilização do MySQL e EF Core no ASP.NET Core.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Pacote necessário para trabalhar com EF e MySQL. | |
//MySql.EntityFrameworkCore | |
//Pacote necessário para trabalhar com Migrations. | |
//Microsoft.EntityFrameworkCore.Tools | |
public class MyDbContext: DbContext { | |
//Construtor. | |
public MyDbContext(DbContextOptions < MyDbContext > options): base(options) {} | |
public DbSet < Person > Person { | |
get; | |
set; | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) { | |
base.OnModelCreating(modelBuilder); | |
//Informando o tipo da coluna Age na tabela Pessoa. | |
modelBuilder.Entity < Person > (e => e.Property(o => o.Age).HasColumnType("tinyint(3)").HasConversion < short > ()); | |
//Informando o tipo da coluna IsPlayer na tabela Pessoa. | |
modelBuilder.Entity < Person > (e => e.Property(o => o.IsPlayer).HasConversion(new BoolToZeroOneConverter < Int16 > ()).HasColumnType("bit")); | |
} | |
} | |
//------------------------------------------- | |
public class Startup { | |
public Startup(IConfiguration configuration) { | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { | |
get; | |
} | |
public void ConfigureServices(IServiceCollection services) { | |
//Adicionando o DbContext no serviço de DI. | |
services.AddDbContext < MyDbContext > (options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection"))); | |
services.AddControllersWithViews(); | |
} | |
//... | |
} | |
/* | |
server=localhost=;user=root;database=testeDb;password=senha;port=3306 | |
*/ | |
//------------------------------------------- | |
public class Person { | |
public int Id { | |
get; | |
set; | |
} | |
[MaxLength(50)] | |
public string FirstName { | |
get; | |
set; | |
} | |
[MaxLength(50)] | |
public string LastName { | |
get; | |
set; | |
} | |
public int Age { | |
get; | |
set; | |
} | |
public bool IsPlayer { | |
get; | |
set; | |
} | |
} | |
//------------------------------------------- | |
//Adicionando uma migration. | |
//PM> add-migration Initial | |
//Após realizar modificações nos models é necessário executar o comando: | |
//PM> update-database | |
//------------------------------------------- | |
public class HomeController: Controller { | |
private readonly ILogger < HomeController > _logger; | |
private readonly MyDbContext _context; | |
public HomeController(ILogger < HomeController > logger, MyDbContext context) { | |
_logger = logger; | |
_context = context; | |
} | |
public IActionResult Index() { | |
var people = _context.Person.ToLisst(); | |
return View(people); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment