Fluent NHibernate - Delete a related object when no explicit relationship exists in the model

Posted by John Price on Stack Overflow See other posts from Stack Overflow or by John Price
Published on 2010-04-23T03:54:45Z Indexed on 2010/04/23 4:03 UTC
Read the original article Hit count: 273

Filed under:
|

I've got an application that keeps track of (for the sake of an example) what drinks are available at a given restaurant. My domain model looks like:

class Restaurant {
    public IEnumerable<RestaurantDrink> GetRestaurantDrinks() { ... }
    //other various properties
}

class RestaurantDrink {
    public Restaurant Restaurant { get; set; }
    public Drink { get; set; }
    public string DrinkVariety { get; set; }  //"fountain drink", "bottled", etc.
    //other various properties
}

class Drink {
    public string Name { get; set; }
    public string Manufacturer { get; set; }
    //other various properties
}

My db schema is (I hope) about what you'd expect; "RestaurantDrinks" is essentially a mapping table between Restaurants and Drinks with some extra properties (like "DrinkVariety" tacked on).

Using Fluent NHibernate to set up mappings, I've set up a "HasMany" relationship from Restaurants to RestaurantDrinks that causes the latter to be deleted when its parent Restaurant is deleted.

My question is, given that "Drink" does not have any property on it that explicitly references RestaurantDrinks (the relationship only exists in the underlying database), can I set up a mapping that will cause RestaurantDrinks to be deleted if their associated Drink is deleted?

© Stack Overflow or respective owner

Related posts about fluent-nhibernate

Related posts about c#