views:

25

answers:

1

Suppose we have two entities Blog and Post that Blog has many Posts. Post is saved indirectly through Blog. When I override OnUpdate in Post, it causes to save Posts that have null Blog. In the other hand overriding OnUpdate in Post causes not saving it properly. Someone else have had same problem.

The code is:

protected override void OnUpdate()
{
    UserModified = "UserModified";
    DateModified = DateTime.Now;

    base.OnUpdate();
}
A: 

Found a work-around myself. If set patent of children explicitly, it will work. Consider following code:

[ActiveRecord(Lazy = true)]
public class Lookup : ActiveRecordBase<Lookup>
{
    [HasMany(typeof(LookupItem), Cascade = ManyRelationCascadeEnum.All)]
    public virtual IList Items { set; get; }

    //other properties...
}


[ActiveRecord(Lazy = true)]
public class LookupItem : ActiveRecordBase<LookupItem>
{
    [BelongsTo("Lookup_id")]
    public virtual Lookup ContainerLookup { set; get; }

    //other properties...
}

void SaveLookup()
{
    Lookup lookup = GetLookup();
    LookupItem lookupItem = new LookupItem()
    {
        Title = LookupItemName,
        ContainerLookup = lookup
    };
    lookup.Items.Add(lookupItem);
    lookup.Save();
}
afsharm