1

I've got a many-to-many relationship set up via AutoMapping. Now, the save and updates work fine as expected, however the DiscountGroups are not being loaded in DiscountDay when I get the entities afterwards. I cannot for the life of me work out why the _discountGroups list is always empty, even though it's all correct in the database.

I've seen suggestions about using ISet rather than IList however it doesn't seem to make any difference in my case, neiter does using .Not.LazyLoad() in the mapping. Removing AsBag() and AsSet() also makes no difference.

The Entities

public class DiscountDay
{
    public virtual DayOfWeek DayOfWeek { get; set; }

    public virtual Discount Discount { get; set; }

    private readonly IList<DiscountGroup> _discountGroups = new List<DiscountGroup>();

    public virtual IEnumerable<DiscountGroup> DiscountGroups
    {
        get { return _discountGroups; }
        set { }
    }
}

public class DiscountGroup
{
    public virtual string Name { get; set; }

    private readonly IList<DiscountDay> _discountDay = new List<DiscountDay>();

    public virtual IEnumerable<DiscountDay> DiscountDay 
    {
        get { return _discountDay; }
    }
}

The Mappings

public class DiscountDayOverride : IAutoMappingOverride<DiscountDay>
{
    public void Override(AutoMapping<DiscountDay> mapping)
    {
        mapping.HasManyToMany( x => x.DiscountGroups )
            .AsSet()
            .Cascade
            .SaveUpdate();
        mapping.Cache.ReadWrite();
    }
}

public class DiscountGroupOverride : IAutoMappingOverride<DiscountGroup>
{
    public void Override(AutoMapping<DiscountGroup> mapping)
    {
        mapping.HasManyToMany( x => x.DiscountDay )
            .AsBag()
            .Inverse();
        mapping.Cache.ReadWrite();
    }
}
4

1 回答 1

1

好吧,我是个十足的笨蛋。DiscountDay 实体上的 DiscountGroups 上的空集导致了问题。出于某种原因,我掩盖了它,只是不认为一个空集合会做任何事情。

更新代码:

public virtual IEnumerable<DiscountGroup> DiscountGroups
{
    get { return _discountGroups; }
}
于 2013-04-04T11:49:52.457 回答