Fluent Mapping - Two entities with collections of the same reference entity

3 posts, 0 answers
  1. Alan
    Alan avatar
    6 posts
    Member since:
    Apr 2012

    Posted 17 Aug 2012 Link to this post

    We have a series of persistent classes all of which have a Collection called Messages which is a collection of Msg entities. Here is our fluent MetadataSource class that uses one sided associations:

    List<MappingConfiguration> configurations = new List<MappingConfiguration>();
     
     //here is the Message mapping
     MappingConfiguration<Msg> msgConfiguration = new MappingConfiguration<Msg>();
     msgConfiguration.MapType(x => new
     {
         EntityId = x.EntityId,
         MessageText = x.MessageText,
         RowVersion = x.RowVersion,
     }).ToTable("Message");
     msgConfiguration.HasProperty(x => x.EntityId).IsIdentity(KeyGenerator.Guid);
     msgConfiguration.HasProperty(x => x.RowVersion).IsVersion();
     
     //here is one entity that has a IList<Msg> property called Messages
     MappingConfiguration<MsgUser> msgUserConfiguration =
    new MappingConfiguration<
    MsgUser>();
     msgUserConfiguration.MapType(x => new
     {
         EntityId = x.EntityId,
         UserName = x.UserName,
         RowVersion = x.RowVersion,
     }).ToTable("MessageUser");
     msgUserConfiguration.HasProperty(x => x.RowVersion).IsVersion();
     msgUserConfiguration.HasProperty(x => x.EntityId).IsIdentity(KeyGenerator.Guid);
     msgUserConfiguration.HasAssociation(p => p.Messages).ToColumn("MsgUserId");
     
     //here is another entity that has a IList<Msg> property called Messages
     MappingConfiguration<AnotherMsgUser> anotherMsgUserConfiguration =
    new MappingConfiguration<
    AnotherMsgUser>();
     anotherMsgUserConfiguration.MapType(x => new
     {
         EntityId = x.EntityId,
         MessageText = x.SomeData,
         RowVersion = x.RowVersion
     }).ToTable("AnotherMessageUser");
     anotherMsgUserConfiguration.HasProperty(x => x.RowVersion).IsVersion();
     anotherMsgUserConfiguration.HasProperty(x => x.EntityId).IsIdentity(KeyGenerator.Guid);
     anotherMsgUserConfiguration.HasAssociation(p => p.Messages).ToColumn("MsgUserId");
     
     //finally we add the configs to the configuration
     configurations.Add(msgUserConfiguration);
     configurations.Add(msgConfiguration);
     configurations.Add(anotherMsgUserConfiguration);

    When this runs and we add msg to either Messages collection the field created in the Message table only contains null values. Eliminating the mapping in either msg user entity fixes the problem. Or changing the name of one of the Associations also resolves the problem but then we get one column in the Message tabgle for each entity class that has Messages. 

    I believe that there must be a workaround (perhaps with inheritance???) but have not been able to get it to work so far. 
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 22 Aug 2012 Link to this post

    Hello Alan,

     This does seem like a bug on our side. Indeed the workaround would be using inheritance and placing the collection in a base class to which the Message class will have an association but let me just make sure I got your scenario right.
    What you want to have is to have two associations pointing to class1 and class2 and both associations should share the same foreign key located in the Message table. Is that correct?

    Greetings,
    Petar
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
  4. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 23 Aug 2012 Link to this post

    Hi Petar and Alan,

    In OA Classic this scenario would typically be handled by having a 1:m mapped as a join table to sit in between the MsgUser and the list of Msg. Thus not utilizing the inverse field as is normally the case when doing 1:m

    On the conceptual level you can ask yourself:
    Will the referenced class in your list be part of a list on any otherother class?
    If so it is a candidate for the 1:m (mapped as a join table)

    I don't know how this is mapped with Fluent though

    /Henrik
Back to Top