开发者

linq to sql dynamic data modify object before insert and update

开发者 https://www.devze.com 2023-01-04 02:20 出处:网络
I\'m using Dynamic Data and LINQ to SQL for some admin pages on a .NET 3.5 web app.All my admin tables have a CreatedBy, CreatedDate, UpdatedBy, and UpdatedDate.

I'm using Dynamic Data and LINQ to SQL for some admin pages on a .NET 3.5 web app. All my admin tables have a CreatedBy, CreatedDate, UpdatedBy, and UpdatedDate.

I'm looking for a way to inject the setting of these properties before the objects are inserted and updated.

I've seen an object_inserting hook if you have a linq to sql datasource in the web form, but I'm using dynamic data...is there an easy way to generically set that? And I've also looked at modifying each of the partial classes for my admin objects, but the closest hook I开发者_JAVA技巧 see is to implement the OnValidate method with the Insert action. Any suggestions? TIA.


David Turvey has published a great example of adding in an OnSaving and OnSaved methods for your entities, check here: Adding OnSaving an OnSaved Events to LINQ to SQL Entities

By implementing the above on your entities, you can extend them with a partial class, e.g.

partial class MyAdminEntity : EntityBase
{
  internal override OnSaving(ChangeAction changeAction)
  {
    if (changeAction == ChangeAction.Insert)
    {
      CreatedBy = "<username>";
      CreatedDate = DateTime.Now;
    }
    else if (changeAction == ChangeAction.Update)
    {
      CreatedBy = "<username>";
      CreatedDate = DateTime.Now;
    }
  }
}


I got tried, add your entity class to app_code, change the class to partial class, it works for me! Hope this help! Reference here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Objects;
using System.Data.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace NorthwindModel
{

    public partial class NorthwindEntities
    {
        partial void OnContextCreated()
        {
            // Register the handler for the SavingChanges event. 
            this.SavingChanges += new EventHandler(context_SavingChanges);
        }

        // SavingChanges event handler. 
        private static void context_SavingChanges(object sender, EventArgs e)
        {
            var objects = ((ObjectContext)sender).ObjectStateManager;

            // Get new objects
            foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Added))
            {
                // Find an object state entry for a SalesOrderHeader object.  
                if (entry.Entity.GetType() == typeof(Employee))
                {
                    var usr = entry.Entity as Employee;

                    // Do your Business Logic here.
                }
            }
        }
    }
}


I know this is an old post, but this could help others in solving their problem.

There are some other ways to do that. You can use this:

public partial class BasicModelDataContext : DataContext
{
        partial void InsertEmployee(Employee instance)
        {
            instance.MyValue = "NEW VALUE";
            Employee.Insert(instance);
        }

        partial void UpdateEmployee(Employee instance)
        {
             instance.MyValue = "NEW Update VALUE";
             Employee.Update(instance);
        }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消