我有一个应用程序可以跟踪(作为示例)给定餐厅提供的饮料。我的域模型如下所示:
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”本质上是 Restaurants 和 Drinks 之间的映射表,具有一些额外的属性(例如添加了“DrinkVariety”)。
使用 Fluent NHibernate 设置映射,我设置了从 Restaurants 到 RestaurantDrinks 的“HasMany”关系,这导致 RestaurantDrinks 在其父 Restaurant 被删除时被删除。
我的问题是,鉴于“Drink”没有任何明确引用 RestaurantDrinks 的属性(该关系仅存在于底层数据库中),我是否可以设置一个映射,如果 RestaurantDrinks 关联,则删除 RestaurantDrinks饮品被删?
更新:我一直在尝试使用“RestaurantDrink”端设置映射
References(x => x.Drink)
.Column("DrinkId")
.Cascade.All();
但这似乎不起作用(我在删除饮料时仍然遇到 FK 违规)。
最佳答案
这个问题的答案表明我想做的事是不可能的:how to define an inverse cascade delete on a many-to-one mapping in hibernate
关于c# - Fluent NHibernate - 当模型中不存在显式父->子关系时级联删除子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2696105/