Created
February 22, 2017 19:49
-
-
Save AlbertoMonteiro/0c23bf54c03b6ccf0d7968b75cb36f79 to your computer and use it in GitHub Desktop.
Mapeando tipos complex no EF sem problemas
This file contains hidden or 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
namespace EfAndCpf | |
{ | |
public class Email | |
{ | |
public string EmailValue { get; private set; } | |
private Email() | |
{ | |
} | |
private Email(string value) | |
{ | |
EmailValue = value; | |
} | |
public static implicit operator Email(string value) | |
=> Parse(value); | |
public static implicit operator string(Email email) | |
=> email.EmailValue; | |
public static Email Parse(string value) | |
{ | |
// ... validations here | |
// throwing exceptions if something goes wrong. | |
return new Email(value); | |
} | |
public bool IsEmpty => EmailValue == null; | |
public override string ToString() | |
=> EmailValue; | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="EntityFramework" version="6.1.3" targetFramework="net45" /> | |
</packages> |
This file contains hidden or 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
namespace EfAndCpf | |
{ | |
public class Pessoa | |
{ | |
public Pessoa(string nome, Email email) | |
{ | |
Nome = nome; | |
Email = email; | |
} | |
private Pessoa() | |
{ | |
} | |
public long Id { get; private set; } | |
public string Nome { get; private set; } | |
public Email Email { get; private set; } | |
public void MudouDeEmail(Email novoEmail) | |
{ | |
Email = novoEmail; | |
} | |
} | |
} |
This file contains hidden or 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 System; | |
using System.Data.Entity; | |
using System.Data.Entity.ModelConfiguration.Conventions; | |
using System.Linq; | |
namespace EfAndCpf | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var ctx = new ExemploContext()) | |
{ | |
ctx.Pessoas.Add(new Pessoa("Alberto", "[email protected]")); | |
ctx.SaveChanges(); | |
Console.WriteLine("Dados salvos"); | |
} | |
using (var ctx = new ExemploContext()) | |
{ | |
var pessoas = ctx.Pessoas.ToArray(); | |
foreach (var pessoa in pessoas) | |
Console.WriteLine(pessoa.Email); | |
pessoas[0].MudouDeEmail("[email protected]"); | |
ctx.SaveChanges(); | |
Console.WriteLine("Email alterado"); | |
} | |
using (var ctx = new ExemploContext()) | |
{ | |
Console.WriteLine("Listando novamente aposa alteração"); | |
var pessoas = ctx.Pessoas.ToArray(); | |
foreach (var pessoa in pessoas) | |
Console.WriteLine(pessoa.Email); | |
} | |
} | |
} | |
public sealed class ExemploContext : DbContext | |
{ | |
public DbSet<Pessoa> Pessoas { get; set; } | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); | |
modelBuilder.ComplexType<Email>(); | |
modelBuilder.Entity<Pessoa>() | |
.Property(p => p.Email.EmailValue).HasColumnName("MeuEmail"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment