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?

4
  • 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. Commented Jun 23, 2011 at 7:02
  • You're right. Possible solution can be completely ignoring values sent from clients and use triggers. Commented Jun 23, 2011 at 7:11
  • The accepted answer here may be a suitable workaround for you. Commented Jul 11, 2011 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. Commented Jul 12, 2011 at 3:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! I'm giving it a try (need to find out how to automate this)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.