Entity Framework how to configure owned-types with multiple sublevels?

Viewed 19

How to configure my entities so that the output table (Person) should look like this :

PersonneId | Name | AddressLine1 | CountryId | DepartmentId | CommonId

With the constraints of foreign keys Example :

  • country1 { id =1 , name = « country1 »} => Department { id =1, name = « department1 »} => common { id =1, name = « common1 »}
  • country1 { id =2 , name = « country2 »} => Department { id =2, name = « department2 »} => common { id =2, name = « common2 »}

I can not write :

PersonneId | Name  | AddressLine1 | CountryId | DepartmentId | CommonId
1          |MyName | AnyAddress   | 1         | 2            | 1         // error

Because Common Id1 is not part of Department Id2

Here are my entities :

public class Personne
{
    public int PersonneId { get; set; }
    public string? Name { get; set; }
    public Address PersonalAddress { get; set; }
   // public Address ProAddress { get; set; }
}

[Owned]    
public class Address
{       
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public virtual Common Common { get; set; }
}    
public class Country
{
    public int CountryId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Department> Departments { get; set; }
}    
public class Department
{
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public Country Country { get; set; }
    public virtual ICollection<Common> Commons { get; set; }
}    
public class Common
{
    public int CommonId { get; set; }
    public string Name { get; set; }
    public Department Department { get; set; }  
}

Thank you and good evening.

0 Answers
Related