3

Say i have the following table:

create table T (
    ID int not null primary key,
    Name varchar(10) not null,
    CreatedAt datetime not null default GETDATE(),
    UpdatedAt datetime not null default GETDATE()
)

and want to use with Linq 2 Sql. It perfectly generates type safe class with both CreatedAt and UpdatedAt properties non-nullable. Now i want to insert a new entry and want them both to be populated by a value from Sql Server, not with DateTime.Now which may differ. Is there a way to somehow send null to Sql Server without making the properties nullable? Also, i don't want to follow this solution as it requires additional network trip. It's very easy with good old SQL - just omit CreatedAt/UpdatedAt columns in the insert statement and you're fine but what are my options with Linq 2 Sql?

UserControl
  • 14,766
  • 20
  • 100
  • 187
  • Once you've solved your insert issue, you're likely to stumble when you want `UpdatedAt` to be set to `GETDATE()` on each subsequent update. – Damien_The_Unbeliever Jun 23 '11 at 07:02
  • You're right. Possible solution can be completely ignoring values sent from clients and use triggers. – UserControl Jun 23 '11 at 07:11
  • The accepted answer [here](http://stackoverflow.com/questions/200617/how-do-i-use-sqls-getdate-and-dateadd-in-a-linq-to-sql-expression/201063) may be a suitable workaround for you. – Stephen Kennedy Jul 11 '11 at 19:34
  • No, it works with select queries only. I tried it and saw that for insert operations it generates extra trip to the server. – UserControl Jul 12 '11 at 03:36

1 Answers1

1

Can you check this link. there is a attribute called IsDbGenerated which can be annotated with your LINQ partial classes.

http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.isdbgenerated.aspx#Y456

DSharper
  • 3,177
  • 9
  • 29
  • 47