locked
Entity Framework Table per Hierarchy throws exception when using trackablecollection RRS feed

  • Question

  • Hi,

    Im having a wiered problem using the table per hierarchy (TPH)  and trackable collection, and I hope someone can help me with. I wil try to simplifiy it to make easier to understand:

    I have class Child1 and Child2 that inherits from Parent class.  these classes will map to the same table in the DB, lets call ParentTable. now the Parent, Child1, Child2 has many to one relation ship to class lets call it "Owner". these classes are  defined as follows:

    public class Parent

    {

    ...

    prublic int Owner_Id { get; set;}

    public Owner  Owner { get; set;}

    ...

    }

    public class Child1: Parent

    {}

    public class Child2: Parent

    {}

     

    public class Owner

    {

    ...

    public TrackableCollection<Parent> ParentItems { get.. set...}

    public TrackableCollection<Child1> Child1Items { get... ; set...;}

    public TrackableCollection<Child2> Child2Items { get...; set..;}

    ...

    }

    For Mapping TPH , I do that inside the ParentConfig File, as follows:

    public class ParentConfig : EntityConfiguration<Parent>

    {

       ParentConfig()

      {

          ....

             //Forieng Key map to OwnerID in the DB

            HasProperty(p=>p.Onwer_Id).HasColumnd("OwnerID")

            // Configure the many to one relationship

            HasRequired(p=>p.Owner).withMany(o=>o.ParentItems).HasForeignKey(p=>p.Owner_Id);

            // Configure the TPH 

          Map<Child1>(p=>p.Requires("Discriminator").HasValue("Child1");

           Map<Child2>(p=>p.Requires("Discriminator").HasValue("Child2");

      }

    }

    The problem Im facing is that when I run the app and I have instance of Owner and I want to populate the Child1 collection from Db Context using the following query:

    "from p in db.Parents.OfType(Child1) where p.R_Id==xx select p"

     I get an exception saying invalid column Name "Owner_Id" ! Now if you notice in  my mapping the Foreign Key column in the DB is called OwnerID and there is no Owner_Id, so Im not sure why its trying to generate the FK form the property named Owner defined in the Parent class. the strange thing is if I take out the trackable collections for both Parent and Child1 , It works and I get the result i need, but I need all collections, am I forgetting to map something , or what Im doing wrong ?

    thanks

     

    Saturday, August 13, 2011 7:47 PM

Answers

  • Hi samsal073,

    Thanks for your feedback.

    >>Regarding the Collections of Child1 and Child2 , I really need both , because I want to use them for two difffenent views/ under the same owner.

    In this way, we need add foreign keys(child1 and child2), it's hardly let owners know the disc to judge which child is.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace THP
    {
      class Program
      {
        static void Main(string[] args)
        {
          using (var context = new MyContext())
          {
            
          }
        }
      }
      public class Parent
      {
        public int ID { get; set; }
        public int Owner_Id { get; set; }
        public Owner Owner { get; set; }
      }
      public class Child1:Parent
      {
        
      }
      public class Child2:Parent
      {
        
      }
      public class Owner
      {
        public int ID { get; set; }
        public ICollection<Parent> ParentItems
        { get; set; }
        public ICollection<Child1> Child1Items { get; set; }
        public ICollection<Child2> Child2Items { get; set; }
      }
      public class MyContext:DbContext
      {
        public DbSet<Parent> Parents { get; set; }
        public DbSet<Owner> Owners { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          modelBuilder.Entity<Parent>().Property(p => p.Owner_Id).HasColumnName("OwnerId");
          modelBuilder.Entity<Parent>().HasRequired(p => p.Owner).WithMany(o => o.ParentItems).HasForeignKey(p => p.Owner_Id);     
          modelBuilder.Entity<Parent>().Map<Child1>(mc => { mc.Requires("disc").HasValue("Child1"); }).Map<Child2>(mc => { mc.Requires("disc").HasValue("Child2"); });
        }
      }
    }
    
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Thursday, September 1, 2011 6:21 AM
    Friday, August 19, 2011 10:06 AM

All replies

  • Hi samsal073,

    Welcome!

    According to your description, you can watch the database's structure, I think there are "Owner_Id" and "Owner_Id1" in Parent table.

    I think there is no need to add Collection<Child1> and Collection<Child2> in Owners.

     

    //public TrackableCollection<Child1> Child1Items { get... ; set...;}
    //public TrackableCollection<Child2> Child2Items { get...; set..;}
    

     


    or add the mapping

     

    HasRequired(p=>p.Owner).withMany(o=>o.ParentItems).HasForeignKey(p=>p.Owner_Id);// ParentItems-->childrens
    

    Have a nice day.

     

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 15, 2011 8:28 AM
  • Hi Alan,

     

    Thanks for your reply , but I dont think that Owner_Id exist in the parent table, only OwnerID. I believe somhow the EF trying to map that column by convention to a hypothitical column that doesnt exist and I dont know how to prevent. Regarding the Collections of Child1 and Child2 , I really need both , because I want to use them for two difffenent views/ under the same owner. if I use one collection, I will endup overriding one collection over the other when switching between the different views, so I need both collection , but I dont kow why when I define one collection it works and and when I try to define two collection it throws the exception above

    Thanks for the help

     

    Monday, August 15, 2011 2:42 PM
  • Hi samsal073,

    Thanks for your feedback.

    >>Regarding the Collections of Child1 and Child2 , I really need both , because I want to use them for two difffenent views/ under the same owner.

    In this way, we need add foreign keys(child1 and child2), it's hardly let owners know the disc to judge which child is.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace THP
    {
      class Program
      {
        static void Main(string[] args)
        {
          using (var context = new MyContext())
          {
            
          }
        }
      }
      public class Parent
      {
        public int ID { get; set; }
        public int Owner_Id { get; set; }
        public Owner Owner { get; set; }
      }
      public class Child1:Parent
      {
        
      }
      public class Child2:Parent
      {
        
      }
      public class Owner
      {
        public int ID { get; set; }
        public ICollection<Parent> ParentItems
        { get; set; }
        public ICollection<Child1> Child1Items { get; set; }
        public ICollection<Child2> Child2Items { get; set; }
      }
      public class MyContext:DbContext
      {
        public DbSet<Parent> Parents { get; set; }
        public DbSet<Owner> Owners { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
          modelBuilder.Entity<Parent>().Property(p => p.Owner_Id).HasColumnName("OwnerId");
          modelBuilder.Entity<Parent>().HasRequired(p => p.Owner).WithMany(o => o.ParentItems).HasForeignKey(p => p.Owner_Id);     
          modelBuilder.Entity<Parent>().Map<Child1>(mc => { mc.Requires("disc").HasValue("Child1"); }).Map<Child2>(mc => { mc.Requires("disc").HasValue("Child2"); });
        }
      }
    }
    
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Thursday, September 1, 2011 6:21 AM
    Friday, August 19, 2011 10:06 AM
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 26, 2011 3:09 AM