locked
How to map table splitting in EF Code First? RRS feed

  • Question

  • How can I map table splitting with EF Code First? Table splitting for EDMX is described for example here. It allows mapping two entities with 1:1 relation into same table. I know I can do the similar mapping with entity and complex type but the big difference is that complex type can't be lazy loaded (or not loaded at all) which is the main reason for table splitting.
    Tuesday, March 22, 2011 11:07 AM

Answers

  • Here is an example:

    Consider these classes Item and ItemDetail. Item is the principal end and will share a table with ItemDetail:

    public class Item
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ItemDetail ItemDetail { get; set; }
    }
    
    public class ItemDetail
    {
      public int Id { get; set; }
      public string Detail { get; set; }
    }
    

     Here is the fluent API code to do table splitting:

    modelBuilder.Entity<Item>().ToTable("Items");
    modelBuilder.Entity<ItemDetail>().ToTable("Items");
    modelBuilder.Entity<Item>().HasRequired(i => i.ItemDetail).WithRequiredPrincipal();

    And there you go, you'll have table splitting. Now you can lazy load your ItemDetail.

    Jeff


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, March 22, 2011 9:19 PM

All replies

  • Bump, can someone please answer this from the Product Team
    Tuesday, March 22, 2011 8:07 PM
  • Here is an example:

    Consider these classes Item and ItemDetail. Item is the principal end and will share a table with ItemDetail:

    public class Item
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ItemDetail ItemDetail { get; set; }
    }
    
    public class ItemDetail
    {
      public int Id { get; set; }
      public string Detail { get; set; }
    }
    

     Here is the fluent API code to do table splitting:

    modelBuilder.Entity<Item>().ToTable("Items");
    modelBuilder.Entity<ItemDetail>().ToTable("Items");
    modelBuilder.Entity<Item>().HasRequired(i => i.ItemDetail).WithRequiredPrincipal();

    And there you go, you'll have table splitting. Now you can lazy load your ItemDetail.

    Jeff


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, March 22, 2011 9:19 PM