2012-04-16 51 views
3

我在ASP.NET动态数据Web应用程序的工作,并有通过默认值,同时插入/更新记录的问题。在我的应用程序的所有表具有以下共同柱:asp.net动态数据网站:通过缺省值,同时插入/更新记录

  • CreatedBy(默认值:已登录用户)
  • CreatedDate(默认值:DateTime.Now)
  • modifiedBy(默认值:已登录用户)
  • ModifiedDate(默认值:DateTime.Now)

我想保持这些列躲在InsertEdit页,并希望该默认值会被插入AUTOMATICA在相应的列中。

请给我建议。

感谢 保罗

回答

0

解决方案,我终于实现:

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) 
    { 
     e.Values.Add("CreatedBy", HttpContext.Current.User.Identity.Name); 
     e.Values.Add("CreatedDate", DateTime.Now); 
    } 

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) 
    { 
     e.OldValues.Add("ModifiedBy", null); 
     e.OldValues.Add("ModifiedDate", null); 
     e.NewValues.Add("ModifiedBy", HttpContext.Current.User.Identity.Name); 
     e.NewValues.Add("ModifiedDate", DateTime.Now); 
    } 
+0

更详细的解释你正在做什么:[在使用实体框架插入ASP.NET动态数据网站之前更新值](http://batesits.com/tag/dynamic-data/) – ofthelit 2016-01-26 14:03:14

0

编号:
Maintaining a Log of Database Changes - Part 1

为了保持对数据库的更改的数据,我们 需要记录每一次插入,更新的历史,并删除一些“历史”表 。除了捕获插入, 更新或删除数据,我们还需要注意哪些用户所做的修改 ,以及它的日期和时间。

更多参考:

Best design for a changelog/auditing database table?
Log changes to database table with trigger

要自定义ORM实体框架检查以下链接:

How to use Default column value from DataBase in Entity Framework?
How to: Execute Business Logic When Saving Changes

+0

感谢您的快速响应。对于历史,我们有单独的交易表。我需要的是假设我不想在插入/编辑动态数据页面时显示一列,即使该列在数据库级别不是NULL。现在插入/更新该实体的记录时,我想在插入/更新查询中传递一些默认值。 – Paul 2012-04-16 16:04:00

+0

Okk..when您创建表时,然后指定列的默认值或使用存储过程指定默认值... – 2012-04-16 16:22:33

+0

..它没有帮助解决我的问题>>> – Paul 2012-04-16 17:08:15

2

我看你在做简单审计看看我的博客文章Basic Auditing for Dynamic Data with Entity Framework 4.x希望有所帮助。

这是从来没有好做这样的事情在页面中,它总是最好做在你的数据模型/层。您可以使用我的A New Way To Do Column Generation in Dynamic Data ...来隐藏所有页面中的列。

+0

对于审计,我们使用了sqlserver 2008审计。我只是想从UI中隐藏这些字段。由/ CreatedDate创建是DB中的必填字段,使用scaffolding = false是不可能的。最后,我通过在FormView1_ItemInserting事件中添加这些字段/值来解决它。无论如何感谢您的信息。我已经阅读了你写在DD上的文章的编号,并得到了很多帮助。非常感谢>> – Paul 2012-04-20 15:37:45

0

我的解决办法是从保罗的要求有点不同。

在文件DynamicData \ PageTemplates \ Insert.aspx.cs我做了修改,以显示我的任何表中的新记录了相同的字段默认值。用户仍然可以插入其他东西。

public partial class Insert : System.Web.UI.Page 
{ 
    protected MetaTable table; 

    protected void Page_Init(object sender, EventArgs e) 
    { 
     table = DynamicDataRouteHandler.GetRequestMetaTable(Context); 
     var values = table.GetColumnValuesFromRoute(Context); 

     // set default values for meta data of new records across all tables 
     // unknown values will be skipped 
     values.Add("creationDate", DateTime.Now); 
     values.Add("modificationDate", DateTime.Now); 
     values.Add("modificationUser", HttpContext.Current.User.Identity.Name.Substring(
      HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1)); 

     FormView1.SetMetaTable(table, values); 
     DetailsDataSource.EntityTypeFilter = table.EntityType.Name; 
    } 
    [...] 
} 

与现有的值编辑记录,我更改了一些DynamicData \ FieldTemplates文件。

public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     // ... 

     // show current user as value for the modification user upon editing records 
     if (Column.Name == "modificationUser") 
     { 
      FieldValue = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\\") + 1); 
     } 
    } 
    [...] 
} 

它会在页面上显示更新后的值进行编辑,但是更新后更改不会持续!需要额外更改编辑页面模板:

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) 
    { 
     // make sure a meta data update is always triggered by setting a different old value 
     // required for the edit components 
     if (e.OldValues.Contains("modificationUser")) 
     { 
      e.OldValues["modificationUser"] = string.Empty; 
      e.OldValues["modificationDate"] = DateTime.MinValue; 
     } 
    } 
相关问题