I have an issue working with EntityFramework using Code First configuration. I have two tables :
+---------------+ +-------------+
| T_CONTRACTS | | T_PERSONS |
|---------------| |-------------|
|CONTRACT_ID | |PERSON_ID |
|CUSTOMER_ID | |NAME |
+---------------+ +-------------+
I want to have a single EF entity :
public class Contract
{
public int ContractId { get; set; }
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
Now, I would like to map my two tables on my entity. I order to do that, I used EntityTypeConfiguration.
public class ContractConfiguration : EntityTypeConfiguration<Contract>
{
public ContractConfiguration()
{
ToTable("T_CONTRACTS", "ASSUROL"); //table and schema ALWAYS in uppercase
HasKey(c => c.ContractId);
Property(c => c.ContractId).HasColumnName("CONTRACT_ID").IsRequired();
Property(c => c.CustomerId).HasColumnName("CUSTOMER_ID").IsRequired();
// TODO : WIP, no idea of what i am doing
HasRequired(c => c.CustomerName).WithRequiredPrincipal().Map( ca => {
ca.MapKey("PERSON_ID");
ca.ToTable("T_PERSONS", "ASSUROL");
//Property(c => c.CustomerName).HasColumnName("NAME");
});
}
}
And here come the crap, i dont know how to achieve the mapping.
-How to map a entity fields to two tables ?
-How to join two tables with different column name for the foreign key (Here CUSTOMER_ID and PERSON_ID) ?
Thanks a lot,
PS : I know we can do this by making two entities with data annotation. I would like to avoid data annotation (because of separation of concern) and I would like to keep a single entity.