2010-07-12 50 views
0

假設我們有兩個實體BlogPostBlog有很多Post s。 Post通過Blog間接保存。當我在Post中覆蓋OnUpdate時,它會導致保存爲空Post s Blog。另一方面,重寫OnUpdatePost導致不能正確保存它。其他人have had same problem爲什麼ActiveRecordHooksBase.OnUpdate不能在級聯模式下工作?

的代碼是:

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

    base.OnUpdate(); 
} 

回答

0

發現自己一個變通。如果明確地設置了兒童專利,它將起作用。考慮以下代碼:

[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(); 
} 
相關問題