Map Object value to Entity Core

Asked

Viewed 193 times

1

I have the class

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

I am trying to map in EF Core using the following code

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

When I try to run the update the 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 well.

Other mappings of the same Entity are working.

1 answer

1


To map in Entity Framework Core is different, the resource used is Property types (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; }
}

Setup 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 this data the same belongs automatically to table People as explained in the documentation

People

Id
Name
Endereco
NomeExibicao

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.