I have an SQL Server DB with a table with these fields:
- A
bit
with the default value 1, NOT NULL
.
- A
smalldatetime
with the default value gettime()
, NOT NULL
.
- An
int
with no default value, IDENTITY
, NOT NULL
.
When I generate Linq to SQL for this table, the following happens:
- The
bit
is given no special treatment.
- The
smalldatetime
is given no special treatment.
- The
int
is marked as IsDbGenerated
.
This means that when I make inserts using Linq to SQL, the following will happen:
- The
bit
will be sent as 0, overriding the default value. Right?
- 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?
- The
IsDbGenerated
int
will not be sent; the DB will generate a value which Linq to SQL will then read back.
What changes do I have to make to make this scenario work?
To summarize: I want non-nullable fields with DB-assigned default values, but I don't want them IsDbGenerated
if it means I cannot provide values for them when making updates or inserts using Linq to SQL. I also do not want them IsDbGenerated
if it means I have to hand-modify the code generated by Linq to SQL.
EDIT: The answer seems to be this is a limitation in the current Linq to SQL.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…