【问题标题】:linq to sql dynamic data modify object before insert and updatelinq to sql 动态数据在插入和更新之前修改对象
【发布时间】:2010-06-18 05:49:49
【问题描述】:

我正在为 .NET 3.5 Web 应用程序上的一些管理页面使用动态数据和 LINQ to SQL。我所有的管理表都有 CreatedBy、CreatedDate、UpdatedBy 和 UpdatedDate。

我正在寻找一种在插入和更新对象之前注入这些属性设置的方法。

如果您在 web 表单中有一个 linq to sql 数据源,我已经看到了一个 object_inserting 钩子,但我使用的是动态数据......有没有简单的方法来通用设置它?而且我还研究了为我的管理对象修改每个部分类,但我看到的最接近的钩子是使用 Insert 操作实现 OnValidate 方法。有什么建议? TIA。

【问题讨论】:

    标签: c# asp.net linq-to-sql dynamic-data


    【解决方案1】:

    David Turvey 发布了一个很好的示例,为您的实体添加 OnSaving 和 OnSaved 方法,请在此处查看:Adding OnSaving an OnSaved Events to LINQ to SQL Entities

    通过在您的实体上实现上述内容,您可以使用部分类扩展它们,例如

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

    【讨论】:

      【解决方案2】:

      我试过了,将你的实体类添加到 app_code,将类更改为部分类,它对我有用!希望这有帮助! 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.
                      }
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        我知道这是一篇旧帖子,但这可以帮助其他人解决他们的问题。

        还有其他一些方法可以做到这一点。 你可以使用这个:

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

        【讨论】:

          猜你喜欢
          • 2010-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-21
          相关资源
          最近更新 更多