Flat Inheritance with One-to-Many association

2 posts, 0 answers
  1. Andy
    Andy avatar
    1 posts
    Member since:
    Apr 2013

    Posted 17 Apr 2014 Link to this post

    I am having problems creating a model that uses Flat inheritance of a base type to multiple other classes.  Basically I have a base type of “Addresses”,  I also have other entity types, Corporations, Customers, Locations, Providers……  I want to store all of the address for those entities in the Addresses table. Each one of the entities may have multiple different addresses.  I have established child classes of Addresses with the discriminator column set (ex, CorpAddresses, CustomerAddresses) and established an association between the entities and the respected type and set managed to true.  All of the associations are set to use the same “Parent_ID”, and I have a feeling that is the problem. 


    When I try to insert any address besides whatever association I set up first,  I get Foreign Key constraint exceptions.  Is there another way that I can get around this or I going to be stuck using multiple back end address tables.  I was wanting to keep them all in one table if possible to make querying for addresses and spatial mapping entities a little easier

    Thanks for the help in advance.  
  2. Boris Georgiev
    Boris Georgiev avatar
    190 posts

    Posted 22 Apr 2014 Link to this post

    Hi Andy,

    If I correctly understand your scenario, you have one-to-many association where on the source side you have the Addresses class which is a base class and in the target side you want to have a navigation collection property from this base type. Unfortunately you cannot create a navigation collection from the base type and use it as you can use a polymorphic references, this is not supported by Telerik Data Access.

    Still for Flat Inheritance there is a workaround which you can use, but you can use it only to get the related addresses from the database and you will not be able to add a new address in the navigation collection. Let's assume that you have an association between two tables - Addresses and Customers and in the Addresses table there is a Foreign Key column CustomerId. For Flat Inheritance you can get all entities with the context.GetAll<T>() method. So in your case you can create an extension method in the EntitiesModel class, which returns all addresses related to one Customer:
    public partial class EntitiesModel
        public IEnumerable<Addresses> GetAddresses(Customer customer)
            this.GetAll<Addresses>().Where(adr => adr.CustomerId == customer.Id);

    I hope that helps. If any other questions arise or you want to achieve something different, do not hesitate to contact us again.

    Boris Georgiev
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. DevCraft banner
Back to Top