Question

I have a complex entity with two collections one to many, I would like to update them how can I do it?

Here is my mapping:

 internal sealed class LeadMap : ClassMap<Lead> {
    public LeadMap() {
        Id(x => x.Id).GeneratedBy.Identity().Column("Id");
        Map(x => x.Notes, "Notes");
        Map(x => x.Source, "Source");
        Map(x => x.BusinessName, "BusinessName");
        Map(x => x.Telephone, "Telephone");
        Map(x => x.UpdateDate, "UpdateDate");
        Map(x => x.ConversationDate, "ConversationDate");
        Map(x => x.IsConverted, "IsConverted");
        Map(x => x.InsertDate, "InsertDate").Generated.Insert();

        // contact details mapping
        Component(x => x.ContactDetails, contact => {
            contact.Map(x => x.Name, "ContactName");
            contact.Map(x => x.Email, "ContactEmail");
            contact.Map(x => x.Telephone, "ContactTelephone");
        });

        // City: 1 : 1
        References(x => x.City).LazyLoad().Column("CityId");

        // MeetingEvent: 1 : 1
        References(x => x.MeetingEvent).LazyLoad().Column("MeetingEventId").Cascade.All();

        // Agent: 1 : 1
        References(x => x.Agent).LazyLoad().Column("AgentId");

        // AutoStatus: 1 : 1
        References(x => x.AutoStatus).LazyLoad().Column("AutoStatusId");

        // ManagementStatus: 1 : 1
        References(x => x.ManagementStatus).LazyLoad().Column("ManagementStatusId").Cascade.All();

        // Activities: * : *
        HasManyToMany(x => x.Activities)
          .Cascade
          .All()
          .LazyLoad()
          .NotFound.Ignore()
          .ParentKeyColumn("LeadId")
          .ChildKeyColumn("ActivityId")
          .Table("LeadsToActivities");

        // Areas: * : *
        HasManyToMany(x => x.Areas)
          .Cascade
          .All()
          .LazyLoad()
          .NotFound.Ignore()
          .ParentKeyColumn("LeadId")
          .ChildKeyColumn("AreaId")
          .Table("LeadToAreas");

        Table("Leads");
    }
}

public sealed class LeadsToActivitiesMap : ClassMap<LeadsToActivities> {
    public LeadsToActivitiesMap() {
        Id(x => x.Id).GeneratedBy.Identity().Column("Id");
        Map(x => x.ActivityId, "ActivityId");
        Map(x => x.LeadId, "LeadId");
        Table("LeadsToActivities");
    }
}

public sealed class AreasToLeadsMap : ClassMap<AreasToLeads> {
    public AreasToLeadsMap() {
        Id(x => x.Id).GeneratedBy.Identity().Column("Id");
        Map(x => x.AreaId, "AreaId");
        Map(x => x.LeadId, "LeadId");
        Table("LeadToAreas");
    }
}

Actually it ignores my update, what I'm doing wrong?

Was it helpful?

Solution

This is a strange mapping. When mapping many-to-many, the relation-table (LeadsToActivities) is managed by NH and doesn't need to be mapped as entity again.

What you actually implemented in C# is a one-to-many collection of LeadsToActivities mapped using many-to-many (which created another relation table).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top