Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq-to-SQL ignores SQL Server default value

Tags:

When using Linq-to-SQL, adding a column to an existing table, and setting a default value on that new column, it seems that Linq to SQL ignores the default value. Has anyone else experienced this behaviour? Is there a way to fix it, so that Linq-to-SQL or SQL Server automatically sets the default value?

The column I added was of type Bit, with a default value set to 1.

like image 721
jao Avatar asked Jul 13 '09 17:07

jao


Video Answer


1 Answers

I have now definitively fixed this by using an example from this blog.

partial void OnCreated() {     if (this.DateTimeCreated == null) {            this.DateTimeCreated = DateTime.Now;     } } 

I needed to pass this into a partial datacontext class, as the default one is automatically overwritten every time you change something in the dbml.

like image 138
jao Avatar answered Oct 08 '22 01:10

jao