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

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

2 Answers 40 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alan
Top achievements
Rank 1
Alan asked on 17 Aug 2012, 07:16 PM
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 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 22 Aug 2012, 03:02 PM
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!
0
IT-Als
Top achievements
Rank 1
answered on 23 Aug 2012, 10:58 AM
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
Tags
General Discussions
Asked by
Alan
Top achievements
Rank 1
Answers by
PetarP
Telerik team
IT-Als
Top achievements
Rank 1
Share this question
or