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 thevirtual
inpublic virtual Estado Estado { get; set; }
, after all it is a navigation property.– Tobias Mesquita
take a look at this model obtained through the
Code First from database
– Tobias Mesquita
@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]?– Renan
this model was generated by VS from the Bank.
– Tobias Mesquita
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 EntityCliente
in the Template or by creating a tableEndereco
in the Database.– Tobias Mesquita