Reputation: 2594
I have two entity classes (with a common interface, for easy automapping), where one has a generic reference to the other:
public interface IModelClass
{
Guid Id { get; set; }
}
class Order : IModelClass
{
public virtual Guid Id { get; set; }
public virtual ISet<Attachment> Attachments { get; set; }
}
class Attachment : IModelClass
{
public virtual Guid Id { get; set; }
public virtual IModelClass AttachedTo { get; set; }
}
I can define the generic reference with ReferencesAny
:
mapping.ReferencesAny(x => x.AttachedTo)
.EntityTypeColumn("entity_type")
.EntityIdentifierColumn("entity_id")
.IdentityType<Guid>()
.MetaType<string>()
.AddMetaValue<Order>("ORDER")
.Cascade.None();
And this works fine, as long as I only work with attachments directly and don't try to do things from the Order
side. What I want though, is to map the reverse of the relationship - i.e. what would normally be handled by:
mapping.HasMany(x => x.Attachments)
so that I can, say, add attachments to the Order's collection and get them persisted without having to manually go through and set up/save each of the attachments (and I'd have to start passing the session around to get the attachments committed in the same transaction). This doesn't work - it just works normally and creates an order_id
column on the attachment table.
Is there some way to set up this mapping so that it 'works'?
Upvotes: 0
Views: 233
Reputation: 30813
mapping.HasMany(x => x.Attachments)
.KeyColumn("entity_id") // to give it the right column to use
.Where("entity_type = 'ORDER'") // makes sure that only attachments for the order are loaded
.Inverse()
.Cascade.AllDeleteOrphan(); // or .Cascade.All(); if Attachments should stay without
Upvotes: 2