Fluent Foreign Key Association For Table Using Composite Primary Key

Thread is closed for posting
1 posts, 0 answers
  1. Fred
    Fred avatar
    3 posts
    Member since:
    Aug 2014

    Posted 04 Feb 2017 Link to this post

    I have a table with two primary ki

    var siteContactConfiguration = new MappingConfiguration<SiteContact>();
                siteContactConfiguration.MapType(sc => new
                {
                    ContactId = sc.ContactId,
                    SiteId = sc.SiteId
                }).ToTable(tableName: "SiteContact");
                siteContactConfiguration.HasProperty(sc => sc.ContactId).IsIdentity();
                siteContactConfiguration.HasProperty(sc => sc.SiteId).IsIdentity();
                siteContactConfiguration.HasIndex(sc => new
                {
                    sc.ContactId,
                    sc.SiteId
                }).WithName(indexName: "PRIMARY");
                siteContactConfiguration.
                    HasIndex().
                    WithMember(sc => sc.SiteId).
                    Ascending().
                    WithName(indexName: "FK_Site_SiteContact_idx");
                siteContactConfiguration.
                    HasAssociation(sc => sc.Site).
                    WithOpposite(s => s.SiteContacts).
                    HasConstraint((sc, s) => sc.SiteId == s.SiteId).
                    IsRequired();
                configurations.Add(siteContactConfiguration);

    I need to know how to create the foreign key reference in the following configuration:

    var contactConfiguration = new MappingConfiguration<Contact>();
                contactConfiguration.MapType(ct => new
                {
                    ContactId = ct.ContactId,
                    FirstName = ct.FirstName,
                    LastName = ct.LastName,
                    Suffix = ct.Suffix,
                    EmailAddress = ct.EmailAddress,
                    IsPlantManager = ct.IsPlantManager,
                    IsActive = ct.IsActive
                }).ToTable(tableName: "Contact");
                contactConfiguration.HasProperty(ct => ct.ContactId).IsIdentity();
                contactConfiguration.
                    HasAssociation(ct => ct.SiteContact).
                    WithOpposite(sc => sc.Contact).
                    HasConstraint((ct, sc) => ct.ContactId == sc.ContactId).
                    IsRequired();
                configurations.Add(contactConfiguration);

    This is the error message I am currently receiving:

    The field 'siteContact' of class 'Contact' must use nested db-column extensions in a db-ref extension as the class 'SiteContact' has a composite primary key. --> FromMetadataContainer/namespace/class[Contact]/field[siteContact]/db-column

    I have searched through the online documentation, and I cannot find any reference to a nested db-column extension.

Back to Top