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

Fluent Foreign Key Association For Table Using Composite Primary Key

0 Answers 37 Views
Design Time (Visual Designer & Tools)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Fred
Top achievements
Rank 1
Fred asked on 04 Feb 2017, 06:10 PM

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 -column extension.

Tags
Design Time (Visual Designer & Tools)
Asked by
Fred
Top achievements
Rank 1
Share this question
or