Map Object Object in Entity Core

1

I have the class

public class Email  
{
     public string Endereco { get; }
     public string  NomeExibicao{ get; }
}

I'm trying to map to EF Core using the following code

 builder.Property(e => e.Email.Endereco)
            .HasColumnName("EmailUsuario");

When I try to run the update the following message appears:

  

The expression 'e = > e.Email.Endereco 'is not a valid property expression. The expression should represent a property access: 't = > t.MyProperty '.   Parameter name: propertyAccessExpression

In EF 6 it worked perfectly.

The other mappings of the same Entity are working.

    
asked by anonymous 20.03.2018 / 16:45

1 answer

1

To map in the Entity Framework Core is different, the feature used is Owned types , A basic example for your question :

The two Entities:

public class People
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Email Email { get; set; }
}
public class Email  
{
     public string Endereco { get; set; }
     public string NomeExibicao { get; set; }
}

Configuration Fluent :

builder.ToTable("People")
    .OwnsOne(x => x.Email, x =>
    {
        x.Property(a => a.Endereco)
            .HasColumnName("Endereco")
            .IsRequired()
            .HasMaxLength(50);
        x.Property(a => a.NomeExibicao)
            .HasColumnName("NomeExibicao")
            .IsRequired()
            .HasMaxLength(50);
    });

As in this configuration the table that could be written to this data automatically belongs to the table People as

People

Id
Name
Endereco
NomeExibicao

References

20.03.2018 / 17:03