我如何确保LINQ到SQL不覆盖或违反非空的数据库默认值? [英] How do I ensure Linq to Sql doesn't override or violate non-nullable DB default values?

查看:174
本文介绍了我如何确保LINQ到SQL不覆盖或违反非空的数据库默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与这些字段的表的SQL Server数据库:




  1. A 使用默认值1, NOT NULL

  2. A SMALLDATETIME 使用默认值 GETTIME() NOT NULL

  3. 一个 INT 没有默认值,身份 NOT NULL



当我生成LINQ to SQL的这个表,会发生以下情况:




  1. 给予任何特殊待遇。

  2. SMALLDATETIME 给予任何特殊待遇。

  3. INT 标记为 IsDbGenerated



这意味着,当我提出使用LINQ to SQL插入,会发生以下情况:




  1. 将被发送为0,覆盖默认值。 从右?

  2. SMALLDATETIME 将发送一个未初始化的的System.DateTime ,产生在SQL服务器的错误,因为它不与SQL Server smalldatetime的范围下降。 从右?

  3. IsDbGenerated INT 将不被发送;数据库将生成一个值的LINQ to SQL然后将读回。



什么变化我必须做出使?这种情况下工作



要总结一下:我想和DB-分配的默认值非空的字段,但我不想让他们 IsDbGenerated ,如果它意味着要使用LINQ to SQL更新或插入的时候,我不能为他们提供价值。我也不想他们 IsDbGenerated 如果这意味着我必须手工修改由LINQ的生成SQL代码。



编辑:答案似乎是,这是在当前的LINQ到SQL的限制


解决方案
<。 p>的LINQ到SQL生成的类不拿起默认值制约性。



也许在未来,但问题是限制并不总是简单值,他们也可以像 GETDATE标量函数(),所以LINQ会在一定程度必须知道如何翻译的。总之,它甚至没有尝试。这也是东西非常数据库特定的类型。




  • 您可以写一个代码生成器来创建实体局部类,你可以提取默认值constriant。

  • 或者业务层的代码可以从XML文件中设置在构造函数中的默认值,和所有你需要做的就是保持xml文件了最新的。

  • 而不是做在构造函数中的工作,你可以模仿SQL和提交对数据库的更改之前检查变更添加默认值。



在你在长度描述遇到的问题 CodeProject上 - 设置默认值LINQ绑定数据


I have an SQL Server DB with a table with these fields:

  1. A bit with the default value 1, NOT NULL.
  2. A smalldatetime with the default value gettime(), NOT NULL.
  3. An int with no default value, IDENTITY, NOT NULL.

When I generate Linq to SQL for this table, the following happens:

  1. The bit is given no special treatment.
  2. The smalldatetime is given no special treatment.
  3. The int is marked as IsDbGenerated.

This means that when I make inserts using Linq to SQL, the following will happen:

  1. The bit will be sent as 0, overriding the default value. Right?
  2. The smalldatetime will be sent as an uninitialized System.DateTime, producing an error in SQL server since it doesn't fall with the SQL Server smalldatetime range. Right?
  3. The IsDbGenerated int will not be sent; the DB will generate a value which Linq to SQL will then read back.

What changes do I have to make to make this scenario work?

To summarize: I want non-nullable fields with DB-assigned default values, but I don't want them IsDbGenerated if it means I cannot provide values for them when making updates or inserts using Linq to SQL. I also do not want them IsDbGenerated if it means I have to hand-modify the code generated by Linq to SQL.

EDIT: The answer seems to be this is a limitation in the current Linq to SQL.

解决方案

Linq-To-Sql generated classes do not pick up the Default Value Constriants.

Maybe in the future, but the issue is constraints aren't always simple values, they can also be scalar functions like GetDate(), so linq would somehow have to know how to translate those. In short, it doesn't even try. It's also a very database-specific type of thing.

  • You could write a code generator to create entity partial classes where you can extract the default value constriant.
  • Alternatively your business layer code could set the defaults in constructors from an xml file, and all you need to do is keep the xml file up-to-date.
  • Instead of doing the work in constructors, you could emulate sql and add default values by inspecting the ChangeSet before submitting changes to the database.

The issue you are having is described at length in CodeProject - Setting Default Values for LINQ Bound Data

这篇关于我如何确保LINQ到SQL不覆盖或违反非空的数据库默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆