With uni-directional mapping and Cascade.All on a collection, is it possible to save only a child, by setting a valid FK then do a Save or SaveOrUpdate ?
If I create a new order, setting a valid Account_id and do a SaveOrUpdate, the Account_id is set to NULL in the database, while the entity still have the Account_id previously set.
Why? and can I modify perhaps the SaveOrUpdate method to make this work?
public class Account
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Order> Orders { get; set; }
//...
}
public class Order
{
public int Id { get; set; }
public int Account__id { get; set; }
public decimal Price { get; set; }
//...
}
Mappings:
public class AccountMap : ClassMap<Account>
{
public AccountMap()
: base()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
HasMany(x => x.Orders).KeyColumn("Account__id").Cascade.All();
}
}
Repository:
public void SaveOrUpdate(T instance)
{
using (ISession session = FnhManager.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
lock (instance)
{
session.SaveOrUpdate(instance);
transaction.Commit();
}
}
}
}
It looks like you didn’t map
Account__idcolumn in Order mappings class. Try to putto your OrderMap class.