gpt4 book ai didi

c# - 当值不相同时违反主键

转载 作者:行者123 更新时间:2023-11-30 19:37:38 24 4
gpt4 key购买 nike

我的数据库中有以下数据:

enter image description here

注意光标所在的位置,因此该字段在单词后有一些空格。然后我调用以下代码来插入一个新值:

 var tagList = new List<ProductTag> { new ProductTag { Name = "diepvries" } };
var sbCopy = new SqlBulkCopy(_dataContext.Database.Connection.ConnectionString) { BulkCopyTimeout = 60 * 10 };
sbCopy.WriteToServer(tagList.AsDataReader());

但是抛出如下异常

Violation of PRIMARY KEY constraint 'PK_dbo.ProductTags'. Cannot insert duplicate key in object 'dbo.ProductTags'. The duplicate key value is (diepvries).

我还用文本“diepvries2”进行了测试,它起作用了。我做错了什么?

最佳答案

根据 this page ,

"SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, <Comparison Predicate>, General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them."

关于c# - 当值不相同时违反主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37212454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com