Question

Using MVC EF4.1, I am trying to link a table (TableMaster) to TableChildOne (relationship one-to-zero-or-one) and also to TableChildTwo (also one-to-zero-or-one). TableChildOne and TableChildTwo are not directly linked.

TablechildOne and TableChildTwo needs to share the primary key of TableMaster (I read this is not possible, any workarounds?)

I am including an image to make this a bit more clear, not sure if there should be foreign keys added somewhere, this is not an actual model created by the code, but is what i would like. not sure if there should be foreign keys somewhere?

image : http://www.davidsmit.za.net/img/untitled.png

My code below compiles, but when trying to add a controller, I get the error :

"An item with the same key has already been added"

  public class TableMaster
{
    public int TableMasterID { get; set; }

    public DateTime ReportDate { get; set; }

    public virtual TableChildOne TableChildOne { get; set; }
    public virtual TableChildTwo TableChildTwo { get; set; }
}

public class TableChildOne
{
    [Key]
    public int TableMasterID { get; set; }

    public String Description_1 { get; set; }

    public virtual TableMaster TableMaster { get; set; }
}

  public class TableChildTwo
  {
     [Key]
      public int TableMasterID { get; set; }

     public String Description_2 { get; set; }

     public virtual TableMaster TableMaster { get; set; }
}

public class Context : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TableMaster>()
            .HasOptional(p => p.TableChildOne).WithRequired(p => p.TableMaster);

        modelBuilder.Entity<TableMaster>()
            .HasOptional(p => p.TableChildTwo).WithRequired(p => p.TableMaster);

}

When I remove the second table completely, it works fine.

I used the below link as an example (tables OfficeAssignment and Student), which shows how to link a table one-to-zero-or-one. But I have trouble adding another table with the same linkage: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

Any help will be appreciated.

Thanks appelmeester

Was it helpful?

Solution

Could you give more background about why you want to do this? If you are sharing the primary key across three tables you are partitioning data. What development scenario are you trying to address. It sounds like you might be wanting to map an object inheritance, is that right?

If you truly only have a couple of Descriptions, then this is really just one table.

EDIT:

Cool. Because the business context of this request is a bit vague, I can't quite understand still, sorry. If you have a TableMaster and then some child tables, then this sounds like an inheritance tree. So with EF, you can choose many different strategies to model this (TPH, TPT etc). For this, I would suggest looking into TPT because this might allow you to get the granularity for how you want to clean up the data. Also, you get the benefit that the tables will be created, by default, largely like you have specified. Check this out for reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top