C'è un modo per avere più istanze di tipo complesso all'interno dello stesso modello utilizzando il costruttore di modelli Fluent api?Il tipo di struttura complessa di entità multiple istanze multiple in un modello?
public class Contact
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address PersonalAddress { get; set; }
public Address BusinessAddress { get; set; }
}
public class Address
{
public string Street{ get; set; }
public string City{ get; set; }
public string PostalCode{ get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new ContactConfiguration());
modelBuilder.Configurations.Add(new AddressConfiguration());
}
=========================================== =======================================
public class AddressConfiguration : ComplexTypeConfiguration<Address>
{
public AddressConfiguration()
{
//props
this.Property(t => t.Street)
.IsOptional()
.HasColumnName("AddressStreet")
.HasMaxLength(1024);
this.Property(t => t.PostalCode)
.IsOptional()
.HasColumnName("AddressPostalCode")
.HasMaxLength(64);
this.Property(t => t.City)
.IsOptional()
.HasColumnName("AddressCity")
.HasMaxLength(512);
}
}
EDIT (TROVATO lA SOLUZIONE)
Se più istanze di tipo classe complessi sono usati in stesso CF Modello, la configurazione di queste classi è fissato a livello CF modello come questo:
public class ContactConfiguration : EntityTypeConfiguration<Contact>
{
public ContactConfiguration()
{
//props for PersonalAddress instance of Address complex type class
this.Property(t => t.PersonalAddress.Address.Street)
.HasColumnName("PersonalAddressStreet");
this.Property(t => t.PersonalAddress.Address.PostalCode)
.HasColumnName("PersonalAddressPostalCode");
this.Property(t => t.PersonalAddress.Address.City)
.HasColumnName("PersonalAddressCity");
//props for BusinessAddress instance of Address complex type class
this.Property(t => t.BusinessAddress.Address.Street)
.HasColumnName("BusinessAddressStreet");
this.Property(t => t.BusinessAddress.Address.PostalCode)
.HasColumnName("BusinessAddressPostalCode");
this.Property(t => t.BusinessAddress.Address.City)
.HasColumnName("BusinessAddressCity");
}
}