Table Per Type (TPT) : This allows all Parent and child classes to be mapped as entities to different tables. The parent class owns the join primary keys while the child classes hold foreign keys. Note your child class might have its own key too, but the parent key is used internally by rapid to generate the join query.

tpt.png

Base Class

    [Entity("Person")]
    public partial class Person {
        
        private String firstname;                
        private Int32 personid;
        
        
        [Field("FirstName")]
        public virtual String FirstName {
            get {
                return firstname;
            }
            set {
                this.firstname = value;
            }
        }
                
        [Key("PersonId", AutoKey=true)]
        public virtual Int32 PersonId {
            get {
                return personid;
            }
            set {
                this.personid = value;
            }
        }        
    }


Subclass
    [Entity("Employee", RelationColumn = "PersonId")]
    public partial class Employee : Person {
        
        private String employeenumber;        
        private String grade;
                
        [Field("EmployeeNumber", Exclude = "Cancelled")]
        public virtual String EmployeeNumber {
            get {
                return employeenumber;
            }
            set {
                this.employeenumber = value;
            }
        }
        
        [Field("Grade")]
        public virtual String Grade {
            get {
                return grade;
            }
            set {
                this.grade = value;
            }
        }
}

Explanation

If you notice the Person class, it has a property PersonId which is attributed as the Key using Rapid attribute. This class will be the one to hold the primary keys in the inheritance. Only the base class holds the key.

Now all said and done, if you notice the child class Employee , it is also mapped with entity attribute, but with an extra attribute property RelationColumn , this relation column is required if the name of joining column (That is the foreign key in the Employee table ) is different from the primary key in the Person table. For the above mapping, the PersonId key property in the Person class is the same name as the RelationColumn = "PersonId" in the Employee class. This can be re mapped as follows because Rapid will reuse the parent primary key name if you do not specify a RelationColumn in the sublasses entity mapping :


    [Entity("Employee")]
    public partial class Employee : Person {
        
        private String employeenumber;        
        private String grade;
                
        [Field("EmployeeNumber", Exclude = "Cancelled")]
        public virtual String EmployeeNumber {
            get {
                return employeenumber;
            }
            set {
                this.employeenumber = value;
            }
        }
        
        [Field("Grade")]
        public virtual String Grade {
            get {
                return grade;
            }
            set {
                this.grade = value;
            }
        }
}


The above code will still work, so far the primary key column in the Parent PersonId is the same as the Foreign key column in the Employee table. All that said and done. Table per classes in hierarchy was a contribution by Shawn Shaddock. Thanks again.

Last edited Feb 7, 2010 at 7:47 PM by ahmedsalako, version 6

Comments

No comments yet.