Entity Framework CTP5 Code-First: mapping a class to multiple collections of another class

With EF CTP5 Code-First I am trying to map a class model that contains several collections in one class pointing to another class. Here is an example of what I mean:

public class Company
{
    public int CompanyId { get; set; }
    public IList<Person> FemaleEmployees { get; set; }
    public IList<Person> MaleEmployees { get; set; }
}

public class Person
{
    public int PersonId { get; set; }
    public Company Company { get; set; }
}

If I let the database create this model using DbContextwithout further configuration, for example:

public class MyContext : DbContext
{
    public DbSet<Company> Companies { get; set; }
    public DbSet<Person> People { get; set; }
}

... then I get two tables in SQL Server, a simple table Companieswith only a column CompanyIdand a table Peoplewith the following columns ("FKRN" means "Foreign Relationship Relationship Name", as created by EF in SQL Server):

PersonId            int     not nullable
CompanyCompanyId    int     nullable       FKRN: Company_FemaleEmployees
CompanyCompanyId1   int     nullable       FKRN: Company_MaleEmployees
CompanyCompanyId2   int     nullable       FKRN: Person_Company

The last three columns are related to the foreign key to the primary key of the CompanyIdtable Companies.

Now I have a few questions:

  • 1) People? . public Company Company { get; set; } Person, CompanyCompanyId2 , ​​ reference .

  • 2) , Company Person ( ). , CompanyCompanyId CompanyCompanyId1? (, FCompanyId MCompanyId, FemaleEmployees MaleEmployees.)

  • 3) CompanyId People? , Person (, bool IsFemale). FemaleEmployees MaleEmployees, (, ), SQL WHERE IsFemale = true/false AND CompanyId = 1. , EntityFramework . ( FemalePerson MalePerson, Person , , , Table-Per-Hierarchy, , SQL Server.) CompanyId non-nullable, ( ).

!

+5
2
  • (1): EF Company Person FemaleEmployees MaleEmployees Company . , Company, . .

  • (2): EF 4.1 Release Candidate API Fluent ( EF CTP5) Map ForeignKeyNavigationPropertyConfiguration:

    modelBuilder.Entity<Company>()
                .HasMany(c => c.FemaleEmployees)
                .WithOptional()
                .Map(conf => conf.MapKey("FCompanyId"))
                .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Company>()
                .HasMany(c => c.MaleEmployees)
                .WithOptional()
                .Map(conf => conf.MapKey("MCompanyId"))
                .WillCascadeOnDelete(false);
    
  • (3): .

Edit

, : (3) ( ) , : Code First Many to 2 Model Mapping... ( , )

+3

, :

public class Company
{
    public int CompanyId { get; set; }
    public ICollection<Person> Employees { get; set; }
    public IEnumerable<Person> MaleEmployees {
        get
        {
            Employees.Where(x=> !x.IsFemale);
        }
    }
}

public class Person
{
    public int PersonId { get; set; }
    public Company Company { get; set; }
}

CompanyID FK People.

- EF:

context.Entry(companyInstance)
    .Collection(p => p.Employees)
    .Query()
    .Where(u => !u.IsFemale)
    .Load();

, , , , Company.FemaleEmployees? EF

+2

All Articles