Codefirst attributemapping - Inherit from base class

3 posts, 0 answers
  1. n_n
    n_n avatar
    2 posts
    Member since:
    Dec 2013

    Posted 20 Jan 2014 Link to this post

    Hi!

    I'm trying to create my first project using OpenAccess but i'm stuck at a part where I would like to inherit my 'table' classes from 1 base class that contains properties that are in every 'table' class.

    my base class:
    public abstract class DataEntity : IEntity {
        [Column(IsPrimaryKey = true, IsBackendCalculated = true)]
        public int Id { get; set; }
     
        [Column("Created")]
        public DateTime Created { get; set; }
     
        [Column("Modified")]
        public DateTime Modified { get; set; }
     
        [Column("Deleted")]
        public bool IsDeleted { get; set; }
     
        public DataEntity() {
            Created = DateTime.Now;
            Modified = DateTime.Now;
        }
    }

    Example of a 'table' class that inherits the base class:

    [Table(Name = "Products", UpdateSchema=true)]
    public class Product : DataEntity, IEntity {
        public string ProductName { get; set; }
        public decimal Price { get; set; }
    }

    The properties of the base class are not in the database?
    only the following properties are created:

    - products_id (probably automatically created by openaccess)
    - product_name
    - price

    Is it possible to include the properties from the base class in every table-class?
  2. n_n
    n_n avatar
    2 posts
    Member since:
    Dec 2013

    Posted 21 Jan 2014 Link to this post

    I've been able to get it working using:

    [InheritanceMapping(InheritanceStrategy = InheritanceStrategy.Vertical)]

    But this gives me a table in the database that contains the properties of the base class and tables that contain the properties of the 'child' classes.

    For me the prefered method would be to have the base class properties to be included in the child-tables. Going through the documentation this could be achieved by using Horizontal inheritance right?

    But horizontal inheritance doesn't allow me the specify the primarykey in the base class if i'm not mistaking.. cause this throws an exception.

    Is it possible to use horizontal inheritence and have the primarykey defined in the base class?

    Regards,
  3. DevCraft banner
  4. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 22 Jan 2014 Link to this post

    Hello Tyhrstan,

    Generally  Horizontal Inheritance is used when the base class should not be mapped to a table in the database. However, as you have discovered, the identity member for the child classes cannot be inherited from the base class.

    I can suggest you the following workaround. You could use Vertical Inheritance to inherit from your base class. This will allow you to inherit the Identity member of the base class. Then from the Properties window of the base class:
    1. Set the Inheritance Modifier property to Abstract.
    2. Set the Kind property to ReadOnly.

    This setup will allow you to emulate the behavior of Horizontal Inheritance - you will not be able to instantiate or insert instances of the base class. Note that the base class table will still be present in the database as it will contain the data for the inherited properties of your other entities.

    I hope you this helps. If you have any more questions, feel free to post at our forums again.


    Regards,
    Kristian Nikolov
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top