問題描述
在我的 db
表中,我有一個 PK
列 sessId
的 uniqueidentifier
設置了函數 newid()
.但是當我插入一條記錄時,sessId
生成為 00000000-0000-0000-0000-000000000000
.
In my db
table i have a PK
column sessId
of uniqueidentifier
set with the function newid()
. But when i insert a record the sessId
is generated as 00000000-0000-0000-0000-000000000000
.
我認為這是一個系統錯誤,所以我運行 select newid()
它生成了一個新的 id
.但是為什么列被插入那些零?
I thought this was a system error so i ran select newid()
it generated a new id
. but why the column gets inserted with those zeros?
推薦答案
發生的情況是您的 SessId 字段是一個 Guid,它是一個值類型,因此必須有一個默認值并且該值全為零.所以當保存到數據庫時,字段上有一個值(全為零)并且不會觸發數據庫默認值.您可以在使用 Guid.NewGuid()
調用 SaveChanges()
之前給 SessId 一個值,并且不要依賴數據庫來執行此操作.或者,如果您可以找到對實體執行此操作的方法,請將字段設為可空的 Guid,以便它以空值到達數據庫.
What is happening is that your SessId field is a Guid which is a value type and therefore must have a default value and that value is all zeros. So when save to the database, there is a value on the field (all zeros) and the database default is not triggered.
You can give SessId a value before calling SaveChanges()
with Guid.NewGuid()
and don't rely on the database to do it.
Or, if you can find a way to do it on your entity, make the field a nullable Guid so that it arrives at the database as null.
這篇關于為什么會話 ID 的性別為“00000000-0000-0000-0000-000000000000"?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!