views:

137

answers:

1

I have a join table where the original table is a numeric type and the join table key column is a string type. Legacy decision that I am trying to avoid having to change to minimize the risk to the scope of work.

HasManyToMany<Attachment>(x => x.Attachments)
  .Table("ObjectAttachments")
  .ParentKeyColumn("ObjectId")
  .ChildKeyColumn("AttachmentId")
  .Fetch.Select()
  .LazyLoad()
  .AsBag();

The owning class uses a long (numeric) type for the identifier, while the join table uses a string type for the object identifier. How could I map this for it to change the data type on the fly? Is there something I can use to intercept and do the conversion?

Perhaps I can open up the discussion by providing a bit more background. The table relationship can be viewed as such:

Equipment -(r1)- ObjectAttachments -(r2)- Attachments

  • r1 is defined by Equipment.Id = ObjectAttachments.Object_Id AND ObjectAttachments.Class = 'Equipment'
  • r2 is defined by ObjectAttachments.Attachment_Id = Attachment.Attachment_Id

The thing that is throwing me for a loop is Equipment.Id is an integer data type and ObjectAttachments.Object_Id is a string.

UPDATE: Here is how I have proceeded for now - I created a seperate repository for the attachments and created a map that brings in the join table. The repository requires that I convert the equipment identifier to a string. It's not what I was hoping for but it works for now. Here's what that map looks like:

Table("Attachments");
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.MimeType);
Map(x => x.Size);
Map(x => x.Date);
Map(x => x.Content).LazyLoad();
Join("ObjectAttachments", join =>
    {
        join.KeyColumn("Id");
        join.Map(x => x.ObjectId);
    });
+1  A: 

One solution would be to add an extra column to the table with the string id with a formula to return the string id cast to the correct numeric type. This constrains the problem with the database to the database rather than letting it bleed into the code.

mattk