2010-11-29 84 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 
} 
相关问题