SQL Server 2008 GUID列全为0 [英] SQL Server 2008 GUID column is all 0's
问题描述
我希望这是我最后做的一件简单的事...
I'm hoping this is a simple goof I did on my end ...
我在数据库中建立了一个表,如下所示:
I have a table in my database set up like so:
column name: widget_guid
data type: uniqueidentifier
allow nulls: false
default value: newid()
identity: false
row guid: true
在创建记录(通过LINQ to SQL)时,该字段中的值被格式化为GUID,但包含全0
When records are created (via LINQ to SQL) that the values in this field are formatted as a GUID but contain all 0's
我的假设是,当创建新记录时,将为该列自动生成一个guid,就像自动递增的行ID一样.这不是真的吗?任何帮助将不胜感激.
My assumption was that when a new record was created, that a guid would be autogenerated for that column, much like an auto-incrementing row id. Is this not true? Any help would be greatly appreciated.
谢谢.
推荐答案
您需要检查GUID列上的属性-您需要确保的是:
You need to check your properties on the GUID column - what you need to make sure is:
-
Auto Generated Values
设置为True
(因此您基本上告诉Linq-to-SQL数据库将生成该值)
Auto Generated Values
is set toTrue
(so you basically tell Linq-to-SQL that the database will generate the value)
Auto-Sync
应该设置为OnInsert
,以便在调用context.SubmitChanges()
Auto-Sync
should be set to OnInsert
so that your C# object will be populated with the new value after you've called context.SubmitChanges()
使用这两个设置,您应该会获得预期的行为:无需在客户端上设置GUID,数据库将生成一个新值并将其插入,然后在调用.SubmitChanges()
With these two settings, you should get the expected behavior: no need to set the GUID on the client side, the database will generate a new value and insert it, and you'll get it back right after the call to .SubmitChanges()
这篇关于SQL Server 2008 GUID列全为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!