Vertical inheritance foreign key to other inherited table

Thread is closed for posting
3 posts, 0 answers
  1. Jeroen
    Jeroen avatar
    15 posts
    Member since:
    Jun 2015

    Posted 09 Sep 2016 Link to this post



    I have a person base class

     public class Person
            public int ID { get; set; }        
            public string Name { get; set; }
            public bool Archived { get; set; }
            public DateTime DateCreated { get; set; }
            public string CreatedBy { get; set; }
            public DateTime DateLastChanged { get; set; }
            public string LastChangedBy { get; set; }      


    From this base class a Customer and Contact class are derivated:

    public class Contact : Person
            public string FirstName { get; set; }
            public string Preposition { get; set; }
            public bool IsActive { get; set; }

            public virtual int? CustomerID { get; set; }
            private Customer customer;
            public virtual Customer Customer { get { return this.customer; } set { this.customer = value; } }         


     public class Customer : Person
            public string CustomerNr { get; set; }
            public string VATNumber { get; set; }       

            private IList<Contact> contact = new List<Contact>();
            public virtual IList<Contact> Contacts { get { return; } }



    I've created this as vertical inheritance:

    var personMapping = new MappingConfiguration<Person>();
                personMapping.MapType(person => new
                    Name = person.Name,
                    Archived = person.Archived,
                    DateCreated = person.DateCreated,
                    CreatedBy = person.CreatedBy,
                    DateLastChanged = person.DateLastChanged,
                    LastChangedBy = person.LastChangedBy
                personMapping.HasProperty(p => p.ID).IsIdentity(KeyGenerator.Autoinc);


    var contactMapping = new MappingConfiguration<Contact>();
                contactMapping.MapType(contact => new
                    FirstName = contact.FirstName,
                    Preposition = contact.Preposition,
                    IsActive = contact.IsActive,
                    CustomerID = contact.CustomerID
                contactMapping.HasAssociation(c => c.Customer).HasFieldName("customer")
                    .WithOpposite(co => co.Contacts).ToColumn("CustomerID")
                    .HasConstraint((co, c) => co.CustomerID == c.ID)


     var customerMapping = new MappingConfiguration<Customer>();
                customerMapping.MapType(customer => new
                    CustomerNr = customer.CustomerNr,
                    VATNumber = customer.VATNumber,
                    AgentID = customer.AgentID                

    customerMapping.HasAssociation(co => co.Contacts).HasFieldName("contact")
                    .WithOpposite(c => c.Customer).ToColumn("ID")
                    .HasConstraint((co, c) => co.ID == c.CustomerID)





    This creates a SQL Database with 3 tables:





    In the Contact table the field CustomerID is persent, but it is not a foreign key to the Customer table.


    Also when I add a contact to the customer and save the customer, the contact is not added. I think this is related


    My question is: can I create the CustomerID as a foreign key in the Contact table, or should I programmatically enforce the save and relationship


    Thanks in advance



  2. Kristian Nikolov
    Kristian Nikolov avatar
    206 posts

    Posted 14 Sep 2016 Link to this post

    Hi Jeroen,

    Thank you for contacting us.

    We tried to reproduce the issue you have described. Using the code you have provided, we created a Fluent Model and ensured the database schema. The relation between the Customer and Contact is internally maintained by Telerik Data Access and by default foreign key constraints are not created in the database itself. If you wish to change that behavior, you can use this article.

    That being said, this setup should not cause the issue yo have described. When trying to reproduce it by adding a contact to the Contacts collection of a customer, and then saving the customer, the problem did not occur. Both the customer and the contact were saved correctly to the database.

    If you are still experiencing this issue, please provide us with a sample project that isolates and demonstrates it so that we may investigate the reasons behind it and help you with the troubleshooting.

    I hope this helps.

    Kristian Nikolov
    Telerik by Progress
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. Jeroen
    Jeroen avatar
    15 posts
    Member since:
    Jun 2015

    Posted 14 Sep 2016 in reply to Kristian Nikolov Link to this post

    Thank you for your response.

    After a few hours of trying and debugging I found I did not set Active in the Contact class to true or false, I left it NULL.

    After setting that to true or false the Contact class was saved correctly.


    If an error message was displayed, I dit not see it, possibly that some other code catched it, and prevente dit.

    Kind regards



Back to Top