私はFluent NHibernateを使用しています。これは、1対多の関係の古典的なケースです。私は多くのSupplyAmount子供を持つ1人のSupply親を持っています。流暢NHibernate 1人から複数人の子供を預けていない
サプライ親オブジェクトは正しい情報で保存されていますが、親を保存するとその量はデータベースに挿入されません。カスケードが動作しないために私は何をしていますか?
次のように実体がある:
public class Supply : BaseEntity
{
public Guid SupplyId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Comments { get; set; }
public virtual IList<SupplyAmount> Amounts { get; set; }
public Supply()
{
Amounts = new List<SupplyAmount>();
}
public virtual void AddAmount(SupplyAmount amount)
{
amount.Supply = this;
Amounts.Add(amount);
}
}
public class SupplyAmount : BaseEntity
{
public virtual Guid SupplymountId { get; set; }
public virtual Supply Supply { get; set; }
public virtual int Amount { get; set; }
}
とマッピングを次のように
public class SupplyMap : ClassMap<Supply>
{
public SupplyMap()
{
Id(x => x.SupplyId);
Map(x => x.LastName);
Map(x => x.FirstName);
Map(x => x.Comments);
HasMany<SupplyAmount>(x => x.Amounts)
.Inverse().Cascade.SaveUpdate()
.KeyColumn("SupplyAmountId")
.AsBag();
}
}
public class SupplyAmountMap : ClassMap<SupplyAmount>
{
public SupplyAmountMap()
{
Id(x => x.SupplyAmountId);
References(x => x.Supply, "SupplyId").Cascade.SaveUpdate();
Map(x => x.Amount);
}
}
そして、これは私がそれを呼び出す方法です:
public SaveIt()
{
Supply sOrder = Supply();
sOrder.FirstName = "TestFirst";
sOrder.LastName = "TestLast";
sOrder.Comments = "TestComments";
for (int i = 0; i < 5; i++)
{
SupplyAmount amount = new SupplyAmount();
amount.Amount = 50;
amount.Supply = sOrder;
sOrder.AddAmount(amount);
}
// This call saves the Supply to the Supply table but none of the Amounts
// to the SupplyAmount table.
AddSupplyOrder(sOrder);
}
'AddSupplyOrder'コードを投稿できますか? – danyolgiax
AddSupplyOrderは、NHibernateがマッピングとエンティティに基づいて作成するFluentのコールです。この例を読みやすくするためにいくつかのコードを省略しました。 – Kyle
私は次のことを見ています: '.KeyColumn(" SupplyAmountId ")'。それは '.KeyColumn(" SupplyId ")'ではないでしょうか? –