【发布时间】:2017-02-18 21:23:38
【问题描述】:
美好的一天。我无法在同一个查询中保存有孩子的父母。但是孩子们对父母的参考是空的...... 我的数据库中的父级是表“food_in_the_cart”,它的模型在这里:
public class FoodInTheCartModel
{
public virtual int ID { get; set; }
public virtual ClientModel Client { get; set; }
public virtual EstablishmentModel Stocked { get; set; }
public virtual ProductModel DesirableProduct { get; set; }
public virtual IList<CartAdditiveModel> Additives { get; set; } //children
public virtual void AddAdditivesToTheCart(CartAdditiveModel a)
{
a.Cart = this;
Additives.Add(a);
}
public FoodInTheCartModel()
{
this.Additives = new List<CartAdditiveModel>();
}
}
也映射:
public FoodInTheCartMap()
{
Table("food_in_the_cart");
Id(x => x.ID)
.Column("id")
.GeneratedBy.Native("food_in_the_cart_id_seq");
References(x => x.Client)
.Column("fk_id_client")
.Not.Nullable()
.Not.LazyLoad();
References(x => x.DesirableProduct)
.Column("fk_id_product")
.Not.Nullable()
.Not.LazyLoad();
References(x => x.Stocked)
.Column("fk_id_stock")
.Not.Nullable()
.Not.LazyLoad();
HasMany(x => x.Additives)
.Table("cart_additive")
.Cascade.SaveUpdate()
.Cascade.All()
.Inverse()
.Not.LazyLoad();
}
而孩子是cart_additive。 cart_additive 的模型是 CartAdditive 的类型,并且对 food_in_the_cart 的模型有参考,同样映射这个参考是:
References(x => x.Cart)
.Column("fk_id_cart")
.Not.LazyLoad();
这两个表的类型关系是一对多的:food_in_the_cart有很多cart_additive。似乎我尝试了一切来为有孩子的父母保存查询......但是孩子的父母仍然有空值。如何使子级中的父级引用不为空?
【问题讨论】:
标签: c# nhibernate fluent-nhibernate mapping