Complex type can have Entity Type property?

Asked

Viewed 68 times

5

I have a class Address that is Complex Type. She can own a state property that’s a Entity Type?

Class code:

public class Endereco
{
   ...
   public string Logradouro { get; set; }
   public Estado Estado { get; set; }
}

public class Estado
{
   public int Id { get; set; }
   public string Nome { get; set; }
}

Mapping of classes:

public class EnderecoConfiguracao: ComplexTypeConfiguration<Endereco>
{
   public EnderecoConfiguracao()
   {
       ...
       //fields
       Property(e => e.Logradouro).HasColumnName("Logradouro").HasMaxLength(200);
    }
}

public class EstadoConfiguracao: EntityTypeConfiguration<Estado>
{
    public EstadoConfiguracao()
    {            
        //Key
        HasKey(e => e.Id);

        //fields
        Property(e => e.Nome).HasColumnName("Nome").HasMaxLength(50).IsRequired();

        //table
        ToTable("estado");
    }
}

In my model, the Customer has an Address, but when trying to map the Status property of that address the following errors occurred:

1st Error: If I map message status "Hba.HbaTools.Infrastructure.EntityFramework.Status: Name: Each type name in a schema must be Unique. Type name 'State' is already defined."

2nd Error: If I comment on the Status mapping of the message "Unknown column 'Extent1.Address_state_id' in 'field list'"


Since I’m still starting with Entity Framework, I thought about removing Status from the Address class, but if this is the best solution would I be changing my domain model due to framework restrictions? It would be correct to do this?


  • as you are starting with Entity Framework, it might be interesting for you to use the Code First from database and study the generated code... but I can say that I miss the virtual in public virtual Estado Estado { get; set; }, after all it is a navigation property.

  • take a look at this model obtained through the Code First from database

  • @Tobymosque I did not put virtual because I read that Complex Type cannot have navigation property. Proceed? In the example template you sent me in the link, the Address class should not be with the attribute [Complextype]?

  • this model was generated by VS from the Bank.

  • maybe this link will help you understand some aspects Entity Framework 6 (7) vs Nhibernate 4: DDD perspective, according to the author one of the problems of EF, is that it forces him to think about the perspective of the database without an isolation of the domain model. So in your case it would be best to remove the ComplexType, unifying him the Entity Cliente in the Template or by creating a table Endereco in the Database.

1 answer

3


Renan, now I understand your problem. Unfortunately it is not possible to reference a Entidades in Tipos Complexos.

So to maintain this structure, you have to transform Endereco in a Entidade, discard Endereco and move its properties to Cliente or remove the property Estado of Endereco.

Browser other questions tagged

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