2012-04-16 26 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; 
     } 
    } 
相關問題