开发者

Fluent NHibernate one to many not saving children

开发者 https://www.devze.com 2023-03-20 19:37 出处:网络
I am using Fluent NHibernate. This is a classic case of a one to many relationship. I have one Supply parent with many SupplyAmount children.

I am using Fluent NHibernate. This is a classic case of a one to many relationship. I have one Supply parent with many SupplyAmount children.

The Supply parent object is saving with correct info, but the amounts are not getting inserted into the db when I save the parent. What am I doing for the cascade not to work?

The entities are as follows:

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; 开发者_JAVA技巧}
    public virtual Supply Supply { get; set; }
    public virtual int Amount { get; set; }
}

And the mapping as follows:

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);
    }
}

And this is how I call it:

    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);
    }


I know this is an old post but why not...

// This call saves the Supply to the Supply table but none of the Amounts

This comment in SaveIt() indicates you call the save on the Supply and not the amounts. In this case you have your logic the wrong way around.

So to fix this:

SupplyMap -> The Inverse shouldn't be there for Amounts.

HasMany<SupplyAmount>(x => x.Amounts).Cascade.SaveUpdate();

SupplyAmountMap -> remove References(x => x.Supply, "SupplyId").Cascade.SaveUpdate(); Replace it with

References<Supply>(x=>x.Supply);

You should now be right to call the save on your supply object only and it will cascade down to the amounts.

Session.Save(supply);

In your test after you have arrange the supply and supplyamount make sure you call a Session.Flush() after your save to force it in. This isn't as important in code as you will usually run in transactions before recalling the supply object.

Cheers, Choco

Also as a side note it usually not a good idea to be to verbose with fluentmappings. let the default stuff do it thing which is why I would recommend against the column naming hints.

0

精彩评论

暂无评论...
验证码 换一张
取 消