How do I ensure Linq to Sql doesn't override or violate non-nullable DB default values?

前端 未结 4 1649
天命终不由人
天命终不由人 2020-12-09 18:35

I have an SQL Server DB with a table with these fields:

  1. A bit with the default value 1, NOT NULL.
  2. A smalldatetime
4条回答
  •  情深已故
    2020-12-09 18:42

    This means that when I make inserts using Linq to SQL, the following will happen:

    1. The bit will be sent as 0, overriding the default value. Right? - Correct
    2. The smalldatetime will be sent as an uninitialized System.DateTime, producing an error in SQL server since it doesn't fall with the SQL Server smalldatetime range. Right? - What is sent is DateTime.MinValue
    3. The IsDbGenerated int will not be sent; the DB will generate a value which Linq to SQL will then read back. - If DB generated is set then the value is created by the database, if not then Linq expects the user to set the value.

    Your best bet is to set them in the constructor of the object, or on the private fields, if you are not using automatic properties.

提交回复
热议问题