问题描述
在使用Linq-to-SQL时,在现有表中添加一列,并在该新列上设置默认值,Linq to SQL似乎会忽略默认值.有没有其他人经历过这种行为?有没有办法解决此问题,以便Linq-to-SQL或SQL Server自动设置默认值?
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?
我添加的列的类型为Bit,默认值设置为1.
The column I added was of type Bit, with a default value set to 1.
推荐答案
我现在通过使用此博客.
partial void OnCreated() {
if (this.DateTimeCreated == null) {
this.DateTimeCreated = DateTime.Now;
}
}
我需要将其传递给部分数据上下文类,因为每次您更改dbml中的某些内容时,默认类都会被自动覆盖.
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.
这篇关于Linq-to-SQL会忽略SQL Server的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!