views:

13

answers:

1

I'm working with an existing database that has the following structure. Changing the database schema is a last resort.

Products
   Id
   Name

ParentProducts
   ParentId
   ChildId

I don't want an entity for ParentProducts, I have the following for the children property (still need to test it, but that's the concept).

<bag name="Children" lazy="true" table="dbo.ParentProducts" cascade="save-update" inverse="true" >
   <key column="[ChildId]"></key>
   <many-to-many column="[ProductId]" class="Product" />
</bag>

What I'm struggling with is how do I create a Parent property? I'd like to do something like the following, but table isn't a valid attribute for many-to-one.

<many-to-one name="Parent" column="[ParentId]" table="dbo.ParentRelated" class="Policy" />

I could create a bag and only ever look at the first item, but that's more of a hack.

Any ideas? Thanks

+1  A: 

Creating a bag is the easiest solution. And you can give it a clean interface:

protected virtual ICollection<Product> Parents { get; set; }

public virtual Product Parent
{
    get
    {
        return Parents.SingleOrDefault();
    }
    set
    {
        Parents.Clear();
        Parents.Add(value);
    }
}

With this, the rest of the code doesn't need to be aware of the DB/mapping structure.

Diego Mijelshon