If you have created an entity framework model from your database schema you should have:
An
AccountDefinition
class with anAccountDefinition.SDOrganization
reference navigation property because the relationship between the two is apparently one-to-oneA
SDOrganization.AaaPostAddresses
collection navigation property because the relationship is many-to-many. TheSDOrgPostalAddr
table should not appear as model entity because it is only the many-to-many link table
With this model and changing your view model to...
public class CustomerDetails
{
public AccountDefinition AccountDefinition {get; set;}
public SDOrganization SDOrganization {get; set;}
public ICollection<AaaPostalAddress> AaaPostalAddresses { get; set; }
}
...you should be able to get the result using:
var customerDetails = context.AccountDefinitions
.Where(a => a.OrgID == givenOrgID)
.Select(a => new CustomerDetails
{
AccountDefinition = a,
SDOrganization = a.SDOrganization,
AaaPostalAddresses = a.SDOrganization.AaaPostalAddresses
})
.SingleOrDefault();
You can also fetch the same data by eager loading the AccountDefinition
entity together with the needed navigation properties:
var accountDefinition = context.AccountDefinitions
.Include(a => a.SDOrganization.AaaPostalAddresses)
.SingleOrDefault(a => a.OrgID == givenOrgID);