Fluent composite key nested column issue

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

    Posted 09 Feb 2017 Link to this post

    I am having an issue with the fluent mapping for a composite key:

    Database structure:
    TABLE `Contact` (
      `ContactId` int(11) NOT NULL AUTO_INCREMENT,
      `FirstName` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
      `LastName` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
      `Suffix` varchar(6) DEFAULT NULL,
      `EmailAddress` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL,
      `IsPlantManager` tinyint(3) unsigned NOT NULL,
      `IsActive` tinyint(3) unsigned NOT NULL,
      PRIMARY KEY (`ContactId`)
    ) ;
     
    `Site` (
      `SiteId` int(11) NOT NULL AUTO_INCREMENT,
      `SiteCode` varchar(10) CHARACTER SET utf8mb4 NOT NULL,
      `SiteName` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
      `AddressId` int(11) NOT NULL,
      `BusinessId` int(11) DEFAULT NULL,
      `TotalUsers` int(11) DEFAULT NULL,
      `AdContainer` longtext,
      `SapLocation` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL,
      `Ldap` longtext,
      `SapCode` varchar(10) CHARACTER SET utf8mb4 DEFAULT NULL,
      `PrinterCode` varchar(6) CHARACTER SET utf8mb4 DEFAULT NULL,
      `ZoneId` int(11) DEFAULT NULL,
      PRIMARY KEY (`SiteId`),
      UNIQUE KEY `SiteCode_UNIQUE` (`SiteCode`),
      KEY `FK_Site_Address` (`AddressId`),
      KEY `FK_Site_BusinessGroup` (`BusinessId`),
      KEY `FK_Zone_Site_idx` (`ZoneId`),
      CONSTRAINT `FK_Site_Address` FOREIGN KEY (`AddressId`) REFERENCES `address` (`AddressId`) ON DELETE NO ACTION ON UPDATE CASCADE,
      CONSTRAINT `FK_Site_BusinessGroup` FOREIGN KEY (`BusinessId`) REFERENCES `businessgroup` (`BusinessId`) ON DELETE SET NULL ON UPDATE CASCADE,
      CONSTRAINT `FK_Zone_Site` FOREIGN KEY (`ZoneId`) REFERENCES `zone` (`ZoneId`) ON DELETE SET NULL ON UPDATE CASCADE
    );
     
    `SiteContact` (
      `ContactId` int(11) NOT NULL,
      `SiteId` int(11) NOT NULL,
      PRIMARY KEY (`ContactId`,`SiteId`),
      KEY `FK_Site_SiteContact_idx` (`SiteId`),
      CONSTRAINT `FK_Contact_SiteContact` FOREIGN KEY (`ContactId`) REFERENCES `contact` (`ContactId`) ON DELETE NO ACTION ON UPDATE CASCADE,
      CONSTRAINT `FK_Site_SiteContact` FOREIGN KEY (`SiteId`) REFERENCES `site` (`SiteId`) ON DELETE NO ACTION ON UPDATE CASCADE
    );

     

    Fluent Mapping:

    var contactConfiguration = new MappingConfiguration<Contact>();
                contactConfiguration.MapType().WithDataAccessKind(DataAccessKind.ReadWrite).ToTable(tableName: "Contact");
                contactConfiguration.HasProperty(ct => ct.ContactId).IsIdentity(KeyGenerator.Autoinc);
     
                configurations.Add(contactConfiguration);
     
    var siteContactConfiguration = new MappingConfiguration<SiteContact>();
                siteContactConfiguration.MapType().WithDataAccessKind(DataAccessKind.ReadWrite).ToTable(tableName: "SiteContact");
                siteContactConfiguration.HasProperty(sc => sc.ContactId).IsIdentity();
                siteContactConfiguration.HasProperty(sc => sc.SiteId).IsIdentity();
     
    var siteConfiguration = new MappingConfiguration<Site>();
                siteConfiguration.MapType().WithDataAccessKind(DataAccessKind.ReadWrite).ToTable(tableName: "Site");
                siteConfiguration.HasProperty(s => s.SiteId).IsIdentity(KeyGenerator.Autoinc);
     
                siteConfiguration.HasAssociation(s => s.Address).WithLoadBehavior(LoadBehavior.Eager).WithOpposite(a => a.Site).HasConstraint((s, a) => s.AddressId == a.AddressId);
                siteConfiguration.HasAssociation(s => s.BusinessGroup).WithLoadBehavior(LoadBehavior.Lazy).WithOpposite(bg => bg.Sites).HasConstraint((s, bg) => s.BusinessId == bg.BusinessId);
                siteConfiguration.HasAssociation(s => s.Zone).WithLoadBehavior(LoadBehavior.Lazy).WithOpposite(z => z.Sites).HasConstraint((s, z) => s.ZoneId == z.ZoneId);
                configurations.Add(siteConfiguration);
     
                siteContactConfiguration.HasAssociation(sc => sc.Contact).WithLoadBehavior(LoadBehavior.Eager).WithOpposite(ct => ct.SiteContact).HasConstraint((sc, ct) => sc.ContactId == ct.ContactId).IsManaged();
                siteContactConfiguration.HasAssociation(sc => sc.Site).WithLoadBehavior(LoadBehavior.Eager).WithOpposite(s => s.SiteContacts).HasConstraint((sc, s) => sc.SiteId == s.SiteId).IsManaged();
                configurations.Add(siteContactConfiguration);

     

    Error Message:

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