why session id is gendered as #39;00000000-0000-0000-0000-000000000000#39;?(为什么会话 ID 的性别为“00000000-0000-0000-0000-000000000000?)
问题描述
在我的 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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么会话 ID 的性别为“00000000-0000-0000-0000-000
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
