English 中文(简体)
VS2005/VS2008 DataSet 设计器,在具有自动生成 GUID 列的表中插入一行。
原标题:
  • 时间:2009-04-30 12:09:18
  •  标签:

I have a strongly typed DataTable created with the VS2005/VS2008 DataSet designer.

The table has a Primary Key column that is a guid, which gets populated by SQL server. The problem is when I want add a row (or multiple rows) to my DataTable and then call the DataAdapter.Update method (passing in the DataTable). When DataAdapter.Update is called I get a SQL exception saying that I cannot insert NULL into the primary key column.

我该如何告诉设计师这是一个自动生成的列,我不想为新行提供值?我只想让 SQL 生成值。

Am I missing something here, or is this a limitation of the DataSet designer?

我知道如何使用LINQ to SQL实现这一点,但不幸的是,这个项目我没有它可用。

最佳回答

可能是以下之一:

  • If you don t need the column in your DataSet for your app, then remove it.
  • If you want the column but don t care to give it a value, then change it to allow DBNull.
  • You can always turn off constraint enforcement (probably a bad idea): DataSet.EnforceConstraints = false
  • You could fill the column with a surrogate key that does not get sent to the DB.

对于前两个选项,如果您想让设计师方便地将您的结构与数据库保持同步,那么您可以通过程序删除该列或允许空值,也许可以在旁边加上一个“// HACK: ”的注释来解释为什么。

问题回答

你可能在SQL Server表定义中使用了DEFAULT NEWID(),因此问题可能是数据集设计器无法将此列视为自动生成的。

Maybe generating guid in your application code could be a solution? If not, then you could set default value in you Dataset but then you re probably have to also change DataAdapter Insert/Update statements so that this default value doesn t get inserted into Sql Server table.

There could also be some other solution that I m not aware of...





相关问题
热门标签