Whenever I have a class with properties that are of the type of other classes, which at the database level represents a foreign key, will I always need the navigation properties?
And see this example:
Revenda -> ClienteRevenda -> Empresa -> ClienteEmpresa
public class Revenda
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get; set;}
public string Nome {get; set;}
// Navigation Property
public ICollection<ClienteRevenda> Clientes {get; set;}
}
public class ClienteRevenda
{
[Key, Column(0)]
public int RevendaId {get; set;}
[ForeignKey("RevendaId")]
public Empresa Empresa {get; set;}
[Key, Column(1)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get; set;}
public string Nome {get; set;}
// Navigation Property
public ICollection<Empresa> Empresas {get; set;}
}
public class Empresa
{
[Key, Column(0)]
public int RevendaId {get; set;}
[ForeignKey("RevendaId")]
public Revenda Revenda {get; set;}
[Key, Column(1)]
public int ClienteRevendaId {get; set;}
[ForeignKey("RevendaId, ClienteRevendaId")]
public ClienteRevenda ClienteRevenda {get; set;}
[Key, Column(2)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get; set;}
public string Nome {get; set;}
// Navigation Property
public ICollection<ClienteEmpresa> ClientesEmpresa {get; set;}
}
public class ClienteEmpresa
{
[Key, Column(0)]
public int RevendaId {get; set;}
[ForeignKey("RevendaId")]
public Revenda Revenda {get; set;}
[Key, Column(1)]
public int ClienteRevendaId {get; set;}
[ForeignKey("RevendaId, ClienteRevendaId")]
public ClienteRevenda ClienteRevenda {get; set;}
[Key, Column(2)]
public int EmpresaId {get; set;}
[ForeignKey("RevendaId, ClienteRevendaId, EmpresaId")]
public Empresa Empresa {get; set;}
[Key, Column(3)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get; set;}
public string Nome {get; set;}
}
Note that I am making composite keys because I need unique records.
In this case, would I need to add in the Reseller class the Navigation Properties for Empresa
and ClienteEmpresa
too?
How would this mapping be with the Fluent API ?
public class RevendaMap : EntityTypeConfiguration<Revenda>
{
public RevendaMap()
{
HasKey(p => p.Id);
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
public class ClienteRevendaMap : EntityTypeConfiguration<ClienteRevenda>
{
public ClienteRevendaMap()
{
HasKey(p => new { p.RevendaId, p.Id });
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasRequired(p => p.Revenda).WithMany(r => r.Clientes).HasForeignKey(p => p.RevendaId);
}
}
public class EmpresaMap : EntityTypeConfiguration<Empresa>
{
public EmpresaMap()
{
HasKey(p => new { p.RevendaId, p.ClienteRevendaId, p.Id });
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasRequired(p => p.Revenda).WithMany( ??? ).HasForeignKey(p => p.RevendaId);
HasRequired(p => p.ClienteRevenda).WithMany(cr => cr.Empresas).HasForeignKey(p => p.ClienteRevendaId);
}
}
public class ClienteEmpresaMap : EntityTypeConfiguration<ClienteEmpresa>
{
public ClienteEmpresaMap()
{
HasKey(p => new { p.RevendaId, p.ClienteRevendaId, p.EmpresaId, p.Id });
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasRequired(p => p.Revenda).WithMany( ??? ).HasForeignKey(p => p.RevendaId);
HasRequired(p => p.ClienteRevenda).WithMany( ??? ).HasForeignKey(p => p.ClienteRevendaId);
HasRequired(p => p.Empresa).WithMany(e => e.ClientesEmpresa).HasForeignKey(p => p.EmpresaId);
}
}
Note in the last two mapping classes that they have the WithMany( ??? )
method. My question is if the Revenda
class should have a navigation property for each of the other classes and also asking for these other classes and methods. What should be informed about them?
Or is it the class structure that is wrong, or the mapping that is wrong ...?