How to skip columns of default values ​​for insert / update in Linq To SQL?

How to insert / update a column through Linq To SQL and Linq To SQL use default values? In particular, I am interested in the timestamp field.

I tried setting this column for reading and auto-generating, so it stopped trying to set DateTime.MinValue, but it does not seem to be updated on updates.

+4
source share
3 answers

The default value for the database will only insert the value when creating the row. It will not do anything to update if you do not want to add a trigger.

Alternatively, you can add a partial method to your DataContext class. Add a new file to your project:

public partial class YourDatabaseDataContext { partial void InsertYourTable(YourTable instance) { instance.LastUpdateTime = DateTime.Now; this.ExecuteDynamicInsert(instance); } partial void UpdateYourTable(YourTable instance) { instance.LastUpdateTime = DateTime.Now; this.ExecuteDynamicUpdate(instance); } 
+2
source

You need to set IsVersion = true for the timestamp column. See ColumnAttribute Link .

In the DBML design, for my timestamp columns, the properties are set to

 AutoGenerated=true AutoSync=Always Nullable, Primary Key, and ReadOnly = false SQLDataType = rowversion not null Timestamp = true UpdateCheck = never 

I assume that you really mean the timestamp, not the date and time. If the latter, ignore it.

+5
source

Looks like you just forgot to set AutoSync to always for this property.

+1
source

All Articles