2010-11-29 87 views
4

我有一個表的smalldatetime NOT NULL字段的默認值爲getdate()。在表中創建新記錄時,我不通過LINQ To SQL語法在代碼中設置smalldatetime字段,我只是讓數據庫設置當前日期的默認值。如果我不明確設置它的代碼,它引發以下錯誤:不設置日期屬性到對象導致錯誤

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

如果我設置數據庫默認的,我爲什麼要必須設置它的代碼?當涉及到LINQ To SQL時,我注意到有日期的時髦事物。

+0

相似問題:http:// stackoverflow。問題/ 201706 /如何做,我保證,LINQ到SQL-doesnt覆蓋或違反,非可空數據庫默認-V – bdukes 2010-11-29 16:34:13

回答

3

,而不是設置字段IsDbGenerated,你可能要考慮它的AutoSync值設置爲OnInsertIsDbGenerated不會讓你設置字段的值(這可能是你想要的「創建日期」字段,但不是「最後修改」字段)。但是,如果您使用的是ORM,那麼我會要求您考慮是否要在數據庫和應用程序代碼中使用您的應用程序邏輯。在代碼中實現默認值(通過partial methods,如Insert[Entity])更有意義嗎?

1

您必須設置生成的屬性,以便LINQ to SQL不會爲創建發送其默認值。

該屬性在實體上被稱爲「自動生成的值」。

alt text

+0

好吧,我會給你一個鏡頭,讓你知道。出於好奇,LINQ to SQL發送的默認值是什麼? – Xaisoft 2010-11-29 16:28:07

+0

它使用任何.NET認爲DateTime的默認值。實際上,我認爲它發送了'1月1日0000'。 – 2010-11-29 16:29:55

+0

好吧,你會認爲他們至少會友好地發送當前日期,哈哈。 – Xaisoft 2010-11-29 16:31:17

0

LINQ to SQL不的方式,你可以隨後更新值觀察數據庫的默認值。爲了做到這一點,您需要在代碼中設置默認值。

當創建一個是NOT NULL與數據庫默認新對象,C#將使用默認值,如MinValue數字和日期的,空的GUID(零),等等。你可以考慮到這些情況,並與自己的默認替換值。

這是LINQ To SQL的已知設計問題。對於一個深入的討論,請參閱以下鏈接:

http://www.codeproject.com/KB/linq/SettingDefaultValues.aspx

在應用程序中設置默認值的一些示例代碼:

private void SetDefaults(object LinqObj) 
{ 
    // get the properties of the LINQ Object 
    PropertyInfo[] props = LinqObj.GetType().GetProperties(); 

    // iterate through each property of the class 
    foreach (PropertyInfo prop in props) 
    { 
     // attempt to discover any metadata relating to underlying data columns 
     try 
     { 
      // get any column attributes created by the Linq designer 
      object[] customAttributes = prop.GetCustomAttributes 
      (typeof(System.Data.Linq.Mapping.ColumnAttribute), false); 

      // if the property has an attribute letting us know that 
      // the underlying column data cannot be null 
      if (((System.Data.Linq.Mapping.ColumnAttribute) 
      (customAttributes[0])).DbType.ToLower().IndexOf("not null") != -1) 
      { 
       // if the current property is null or Linq has set a date time 
      // to its default '01/01/0001 00:00:00' 
       if (prop.GetValue(LinqObj, null) == null || prop.GetValue(LinqObj, 
       null).ToString() == (new DateTime(1, 1, 1, 0, 0, 0)).ToString()) 
       { 

        // set the default values here : could re-query the database, 
        // but would be expensive so just use defaults coded here 
        switch (prop.PropertyType.ToString()) 
        { 
         // System.String/NVarchar 
         case "System.String": 
          prop.SetValue(LinqObj, String.Empty, null); 
          break; 
         case "System.Int32": 
         case "System.Int64": 
         case "System.Int16": 
          prop.SetValue(LinqObj, 0, null); 
          break; 
         case "System.DateTime": 
          prop.SetValue(LinqObj, DateTime.Now, null); 
          break; 
        } 
       } 
      } 
     } 
     catch 
     { 
      // could do something here ... 
     } 
    } 
0

爲了解決這個問題,請確保您的LINQ to SQL模型知道您的smalldatetime字段是由數據庫自動生成的。

在LINQ To SQL圖表中選擇表格的字段,並找到屬性窗口。將自動生成的值屬性調整爲True。這將確保該字段不包含在由LINQ To SQL生成的INSERT語句中。

alt text

或者,你必須指定這個自己:

if (newCustomer.DateTimeCreated == null) { 
     newCustomer.DateTimeCreated = DateTime.Now; // or UtcNow 
} 
相關問題