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

Vertical inheritance foreign key to other inherited table

2 Answers 31 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeroen
Top achievements
Rank 1
Iron
Jeroen asked on 09 Sep 2016, 07:03 AM

Hello,

 

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 this.contact; } }

}

 

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
            }).ToTable("Person");
            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
            })
                .Inheritance(Telerik.OpenAccess.InheritanceStrategy.Vertical)
                .ToTable("Contact");
            contactMapping.HasAssociation(c => c.Customer).HasFieldName("customer")
                .WithOpposite(co => co.Contacts).ToColumn("CustomerID")
                .HasConstraint((co, c) => co.CustomerID == c.ID)
                .IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);

 

 var customerMapping = new MappingConfiguration<Customer>();
            customerMapping.MapType(customer => new
            {
                CustomerNr = customer.CustomerNr,
                VATNumber = customer.VATNumber,
                AgentID = customer.AgentID                
            })
                .Inheritance(Telerik.OpenAccess.InheritanceStrategy.Vertical)
                .ToTable("Customer");

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

 

 configuration.Add(personMapping);

configuration.Add(contactMapping);
configuration.Add(customerMapping);

 

This creates a SQL Database with 3 tables:

Person

Contact

Customer

 

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

 

Jeroen

2 Answers, 1 is accepted

Sort by
0
Kristian Nikolov
Telerik team
answered on 14 Sep 2016, 09:31 AM
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.

Regards,
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.
0
Jeroen
Top achievements
Rank 1
Iron
answered on 14 Sep 2016, 09:42 AM

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

 

Jeroen

Tags
Development (API, general questions)
Asked by
Jeroen
Top achievements
Rank 1
Iron
Answers by
Kristian Nikolov
Telerik team
Jeroen
Top achievements
Rank 1
Iron
Share this question
or