This is a migrated thread and some comments may be shown as answers.

Codefirst attributemapping - Inherit from base class

2 Answers 59 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
n_n
Top achievements
Rank 1
n_n asked on 20 Jan 2014, 10:59 AM
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 Answers, 1 is accepted

Sort by
0
n_n
Top achievements
Rank 1
answered on 21 Jan 2014, 10:01 AM
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,
0
Kristian Nikolov
Telerik team
answered on 22 Jan 2014, 12:50 PM
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!
Tags
Data Access Free Edition
Asked by
n_n
Top achievements
Rank 1
Answers by
n_n
Top achievements
Rank 1
Kristian Nikolov
Telerik team
Share this question
or