Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL GETDATE() in insert operations using Linq To Sql

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?

like image 708
UserControl Avatar asked Sep 10 '26 17:09

UserControl


1 Answers

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

like image 177
DSharper Avatar answered Sep 13 '26 15:09

DSharper