Linq2sql missing event model?

Why don't the Table classes created in Dbml contain useful events like

OnBeforeInsert OnBeforeUpdate

OnAfterInsert et al.

Did I miss something?

This question is related to frustration trying to set timestamp columns.

UPDATE

I created the following way to do it neatly, what does everyone think?

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;
    }
}
+3
source share
1 answer

I have had a similar problem lately.

The created class has a partial method for "OnValidate". Just declaring a method in your partial will cause it to call (vb.net does not support partial methods such as C #), or in C # just declaring a partial method.

System.Data.Linq.ChangeAction, : , , .

, , .

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

}
+3

All Articles