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