Click here to Skip to main content
15,893,588 members
Articles / Web Development / ASP.NET

A Beginner's Tutorial on Understanding Table Per Hierarchy(TPH) Inheritance in Entity Framework

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
13 Feb 2013CPOL6 min read 35K   468   20  
This article talks about the Table per Hierarchy inheritance relationship using entity framework.
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

[assembly: EdmSchemaAttribute()]

namespace DataAccessLayer
{
    #region Contexts
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class CarsDbEntities : ObjectContext
    {
        #region Constructors
    
        /// <summary>
        /// Initializes a new CarsDbEntities object using the connection string found in the 'CarsDbEntities' section of the application configuration file.
        /// </summary>
        public CarsDbEntities() : base("name=CarsDbEntities", "CarsDbEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new CarsDbEntities object.
        /// </summary>
        public CarsDbEntities(string connectionString) : base(connectionString, "CarsDbEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new CarsDbEntities object.
        /// </summary>
        public CarsDbEntities(EntityConnection connection) : base(connection, "CarsDbEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region Partial Methods
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<Car> Cars
        {
            get
            {
                if ((_Cars == null))
                {
                    _Cars = base.CreateObjectSet<Car>("Cars");
                }
                return _Cars;
            }
        }
        private ObjectSet<Car> _Cars;

        #endregion
        #region AddTo Methods
    
        /// <summary>
        /// Deprecated Method for adding a new object to the Cars EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToCars(Car car)
        {
            base.AddObject("Cars", car);
        }

        #endregion
    }
    

    #endregion
    
    #region Entities
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="TestDbModel", Name="Car")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    [KnownTypeAttribute(typeof(DiscountedCar))]
    [KnownTypeAttribute(typeof(PreOwnedCar))]
    public abstract partial class Car : EntityObject
    {
        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 CarID
        {
            get
            {
                return _CarID;
            }
            set
            {
                if (_CarID != value)
                {
                    OnCarIDChanging(value);
                    ReportPropertyChanging("CarID");
                    _CarID = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("CarID");
                    OnCarIDChanged();
                }
            }
        }
        private global::System.Int32 _CarID;
        partial void OnCarIDChanging(global::System.Int32 value);
        partial void OnCarIDChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String CarName
        {
            get
            {
                return _CarName;
            }
            set
            {
                OnCarNameChanging(value);
                ReportPropertyChanging("CarName");
                _CarName = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("CarName");
                OnCarNameChanged();
            }
        }
        private global::System.String _CarName;
        partial void OnCarNameChanging(global::System.String value);
        partial void OnCarNameChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Manufacturer
        {
            get
            {
                return _Manufacturer;
            }
            set
            {
                OnManufacturerChanging(value);
                ReportPropertyChanging("Manufacturer");
                _Manufacturer = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Manufacturer");
                OnManufacturerChanged();
            }
        }
        private global::System.String _Manufacturer;
        partial void OnManufacturerChanging(global::System.String value);
        partial void OnManufacturerChanged();

        #endregion
    
    }
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="TestDbModel", Name="DiscountedCar")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class DiscountedCar : Car
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new DiscountedCar object.
        /// </summary>
        /// <param name="carID">Initial value of the CarID property.</param>
        /// <param name="discountRate">Initial value of the DiscountRate property.</param>
        public static DiscountedCar CreateDiscountedCar(global::System.Int32 carID, global::System.Int32 discountRate)
        {
            DiscountedCar discountedCar = new DiscountedCar();
            discountedCar.CarID = carID;
            discountedCar.DiscountRate = discountRate;
            return discountedCar;
        }

        #endregion
        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 DiscountRate
        {
            get
            {
                return _DiscountRate;
            }
            set
            {
                OnDiscountRateChanging(value);
                ReportPropertyChanging("DiscountRate");
                _DiscountRate = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("DiscountRate");
                OnDiscountRateChanged();
            }
        }
        private global::System.Int32 _DiscountRate;
        partial void OnDiscountRateChanging(global::System.Int32 value);
        partial void OnDiscountRateChanged();

        #endregion
    
    }
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="TestDbModel", Name="PreOwnedCar")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class PreOwnedCar : Car
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new PreOwnedCar object.
        /// </summary>
        /// <param name="carID">Initial value of the CarID property.</param>
        /// <param name="yearsOld">Initial value of the YearsOld property.</param>
        public static PreOwnedCar CreatePreOwnedCar(global::System.Int32 carID, global::System.Int32 yearsOld)
        {
            PreOwnedCar preOwnedCar = new PreOwnedCar();
            preOwnedCar.CarID = carID;
            preOwnedCar.YearsOld = yearsOld;
            return preOwnedCar;
        }

        #endregion
        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 YearsOld
        {
            get
            {
                return _YearsOld;
            }
            set
            {
                OnYearsOldChanging(value);
                ReportPropertyChanging("YearsOld");
                _YearsOld = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("YearsOld");
                OnYearsOldChanged();
            }
        }
        private global::System.Int32 _YearsOld;
        partial void OnYearsOldChanging(global::System.Int32 value);
        partial void OnYearsOldChanged();

        #endregion
    
    }

    #endregion
    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions