2008-12-30 50 views
3

为什么在DBML生成的 “表” 类不包含像linq2sql缺少事件模型?

OnBeforeInsert OnBeforeUpdate

OnAfterInsert 等

我失去了一些有用的东西的事件?

这个问题与试图设置时间戳列的沮丧有关。

UPDATE

我创造了这样整齐什么每个人都认为以下方法是什么?

public class Model 
{ 
    internal virtual void OnBeforeInsert() 
    { 
    } 
    internal virtual void OnBeforeUpdate() 
    { 
    } 
} 

public partial class DbDataContext 
{ 
    public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode) 
    { 
     foreach (var insert in this.GetChangeSet().Inserts) 
     { 
      if (insert is Model) 
      { 
       ((Model)insert).OnBeforeInsert(); 
      } 
     } 

     foreach (var update in this.GetChangeSet().Updates) 
     { 
      if (update is Model) 
      { 
       ((Model)update).OnBeforeUpdate(); 
      } 
     } 

     base.SubmitChanges(failureMode); 
    } 
} 

public partial class Address : Model 
{ 
    internal override void OnBeforeInsert() 
    { 
     var created = DateTime.Now; 
     this._Modified = created; 
     this._Created = created; 
    } 
} 
+0

我以前有过这个相同的问题,无法弄清楚,只是使用trigers – Shawn 2008-12-30 05:54:24

回答

3

我最近有类似的问题。

生成的类中存在“OnValidate”的部分方法。简单地声明部分方法会迫使它被调用(vb.net不支持像c#这样的部分方法),或者在c#中简单地声明部分方法。

该方法传递System.Data.Linq.ChangeAction枚举:Delete,Insert,Update或None。

下面是您使用内置部分方法所做的示例。

public partial class Address 
{ 

    private partial void OnValidate(System.Data.Linq.ChangeAction action) 
    { 
     if (action == System.Data.Linq.ChangeAction.Insert) 
     { 
      var created = DateTime.Now; 
      this._Modified = created; 
      this._Created = created; 
     } else if (action == System.Data.Linq.ChangeAction.Update) { 
      this._Modified = DateTime.Now; 
     } 
    } 

}