Good day. I can't save parent with children in same query. But children have null reference for parent... Parent in my DB is table "food_in_the_cart" and model for it is here:
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>();
}
}
Mapping also:
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();
}
And child is cart_additive. Model of cart_additive is type of CartAdditive and has reference for model of food_in_the_cart, also mapping this reference is:
References(x => x.Cart)
.Column("fk_id_cart")
.Not.LazyLoad();
Type relationship between these two tables is one to many: food_in_the_cart has many cart_additive. Seems I tried everything for saving query for parent with children... But children still have null value for parent. How to make parent reference in child not null?
I removed Insert() from has many in FoodInTheCartMap and and added AsBug() there, also allowed null values for fk of food_in_the_cart in cart_additives as for reference called Cart in model for this table. All works this way. Yay.