Fluent NHibernate - 当模型中不存在显式父子关系时级联删除子对象
我有一个应用程序可以跟踪(为了举例)给定餐厅提供的饮料。我的域模型如下所示:
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
}
我的数据库模式(我希望)符合您的期望; “RestaurantDrinks”本质上是餐厅和饮料之间的映射表,具有一些额外的属性(例如附加的“DrinkVariety”)。
使用 Fluent NHibernate 设置映射,我设置了从 Restaurants 到 RestaurantDrinks 的“HasMany”关系,这导致后者在删除其父 Restaurant 时也被删除。
我的问题是,鉴于“Drink”没有任何显式引用 RestaurantDrinks 的属性(该关系仅存在于底层数据库中),我是否可以设置一个映射,如果删除其关联的 Drinks,该映射将导致 RestaurantDrinks 被删除?
更新:我一直在尝试使用“RestaurantDrink”端设置映射
References(x => x.Drink)
.Column("DrinkId")
.Cascade.All();
但这似乎不起作用(删除饮料时我仍然遇到 FK 违规)。
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?
Update: I've been trying to set up the mapping from the "RestaurantDrink" end of things using
References(x => x.Drink)
.Column("DrinkId")
.Cascade.All();
But this doesn't seem to work (I still get an FK violation when deleting a Drink).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题的答案表明我想做的事情是不可能的: 如何在 hibernate 中的多对一映射上定义反向级联删除
The answers to this question suggest that what I want to do isn't possible: how to define an inverse cascade delete on a many-to-one mapping in hibernate