Created
March 19, 2021 19:15
-
-
Save dclobato/51576416b7c3509cba7591b42c6d66ef to your computer and use it in GitHub Desktop.
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
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.Metadata.Builders; | |
using Microsoft.Extensions.Logging; | |
using System; | |
using System.Collections.Generic; | |
namespace EFCore_NM | |
{ | |
class Program | |
{ | |
public class Genero | |
{ | |
public int GeneroID { get; set; } | |
public string GeneroNome { get; set; } | |
public ICollection<FilmeGenero> FilmeGeneros { get; set; } | |
} | |
public class Filme | |
{ | |
public int FilmeID { get; set; } | |
public string TituloOriginal { get; set; } | |
public string TituloNacional { get; set; } | |
public int Duracao { get; set; } | |
public string Sinopse { get; set; } | |
public ICollection<FilmeGenero> FilmeGeneros { get; set; } | |
} | |
public class FilmeGenero | |
{ | |
public int FilmeID { get; set; } | |
public Filme Filme { get; set; } | |
public int GeneroID { get; set; } | |
public Genero Genero { get; set; } | |
} | |
public class FilmesDbContext : DbContext | |
{ | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Filmes;" + | |
"Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;" + | |
"ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); | |
var logSql = new LoggerFactory(); | |
logSql.AddProvider(new MeuLogProvider()); | |
optionsBuilder.UseLoggerFactory(logSql); | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
modelBuilder.ApplyConfiguration(new FilmeGeneroConfiguration()); | |
} | |
public DbSet<Filme> Filmes { get; set; } | |
public DbSet<Genero> Generos { get; set; } | |
public DbSet<FilmeGenero> FilmeGeneros { get; set; } | |
} | |
public class FilmeGeneroConfiguration : IEntityTypeConfiguration<FilmeGenero> | |
{ | |
public void Configure(EntityTypeBuilder<FilmeGenero> builder) | |
{ | |
builder.HasKey(p => new { p.FilmeID, p.GeneroID }); | |
builder.HasOne<Filme>(p => p.Filme) | |
.WithMany(p => p.FilmeGeneros) | |
.HasForeignKey(p => p.FilmeID); | |
builder.HasOne<Genero>(p => p.Genero) | |
.WithMany(p => p.FilmeGeneros) | |
.HasForeignKey(p => p.GeneroID); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment