Linq-to-SQL 忽略 SQL Server 默认值 [英] Linq-to-SQL ignores SQL Server default value
问题描述
当使用 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.
推荐答案
我现在已经通过使用 这个博客.
I have now definitively fixed this by using an example from this blog.
partial void OnCreated() {
if (this.DateTimeCreated == null) {
this.DateTimeCreated = DateTime.Now;
}
}
我需要将它传递给部分 datacontext 类,因为每次更改 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 默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!