I have a table that has a smalldatetime NOT NULL field with a default value of getdate(). When creating a new record in the table, I don’t set the smalldatetime field in code through LINQ To SQL syntax, I just let the database set the default value which is the current date. If I don’t explicitly set it in code, it throws the following error:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
If I am setting a default in the database, why should I have to set it in code? I have noticed funky things with dates when it comes to LINQ To SQL.
Rather than setting the field as
IsDbGenerated, you may want to consider setting itsAutoSyncvalue toOnInsert.IsDbGeneratedwon’t let you set the field’s value ever (which may be what you want for a “created date” field, but not for a “last modified” field).However, if you’re using an ORM, I would ask you to consider whether you want your application logic in both the database and the application code. Does it make more sense to implement the default value in code (via partial methods like
Insert[Entity])?