使用 SubSonic ActiveRecord 插入新行后未从 Identity 列获取键值
我确信我在这里错过了明显的答案,但可以帮忙。我是 SubSonic 的新手,正在使用版本 3。我已经能够查询和插入了,但是我一直不知道如何在插入后取回标识列的值。我看到另一篇文章提到了 Linq 模板。我没有使用那些(至少我不认为我是......?)
TIA
...更新......
所以我一直在通过我的代码进行调试,观察SubSonic代码是如何工作的,我发现了身份的位置列被忽略。我使用 int 作为数据库中 ID 列的数据类型,并将它们设置为身份。由于 int 是 c# 中不可为 null 的数据类型,因此 Add 方法 (public void Add(IDataProviderprovider)) 中的逻辑测试(通过执行 (key==null) 检查键列中是否有值)可能是问题。获取标识字段新值的代码位于“真实路径”中,因为 int 不能为 null,并且我使用 int 作为标识列数据类型,因此该测试永远不会通过。我的对象的 ID 字段中有一个 0,但我没有将其放在那里。我假设它是在对象初始化期间设置的。我在这里偏离基地了吗?答案是更改数据库中的数据类型吗?
另一个问题(更多的是好奇心)。我注意到生成的类中的一些属性是用 ? 声明的。在数据类型之后。我不熟悉这个声明结构...给出了什么?有一些声明为 int (非关键字段),还有一些声明为 int? (关键字段)。这与它们在初始化时的处理方式有关吗?
任何帮助表示赞赏!
- 撞 -
I'm sure I'm missing the obvious answer here, but could use a hand. I'm new to SubSonic and using version 3. I've got myself to the point of being able to query and insert, but I'm stuck with how I would get the value of the identity column back after my insert. I saw another post that mentioned Linq Templates. I'm not using those (at least I don't think I am...?)
TIA
... UPDATE ...
So I've been debugging through my code watching how the SubSonic code works and I found where the indentity column is being ignored. I use int as the datatype for my ID columns in the database and set them as identity. Since int is a non-nullable data type in c# the logical test in the Add method (public void Add(IDataProvider provider)) that checks if there is a value in the key column by doing a (key==null) could be the issue. The code that gets the new value for the identity field is in the 'true path', since an int can't be null and I use ints as my identity column data types this test will never pass. The ID field for my object has a 0 in it that I didn't put there. I assume it's set during the initialization of the object. Am I off base here? Is the answer to change my data types in the database?
Another question (more a curiosity). I noticed that some of the properties in the generated classes are declared with a ? after the datatype. I'm not familiar with this declaration construct... what gives? There are some declared as an int (non key fields) and others that are declared as int? (key fields). Does this have something to do with how they're treated at initialization?
Any help is appreciated!
--BUMP--
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于你的问题的第一部分无法帮助你太多。也许您应该为此创建一个 T4 模板,如下所述:
如何获取与 SubSonic 3 LinqTemplate 插入关联的标识列值?
“?”您在 int 声明后发现的内容引用了可为 null 的类型:
http://msdn.microsoft.com/en-en/library/1t3y8s4s(VS.80).aspx
这基本上意味着该属性可以为空。
Can't help you much with the first part of your question. Probably you should create a T4 template for that, as explained here:
How Can I Get the Identity Column Value Associated with a SubSonic 3 LinqTemplate Insert?
The "?" that you find after the int declaration refers to a nullable type:
http://msdn.microsoft.com/en-en/library/1t3y8s4s(VS.80).aspx
It basically means that the property can be null.
这对我有用,我使用完全相同的设置:
本例中没有错误处理,但你明白我的意思了。
This works for me, I'm using exactly the same setup:
No error handling in this example, but you get my drift.
除了我之前的回答;整数值不能为空。它是一个整数,因为数据库列是主键,因此根据定义它不应为空。不可为 null 的整数的默认值为 0。 查看 C# 中的默认值
Mamoo 对可空类型的引用也值得参考到。因此,您的单元测试应该测试 0 表示失败,大于 0 表示通过。
In addition to my previous answer; an integer value cannot be null. It is an integer because the database column is a primary key, and therefore it should not be null by definition. The default value for a non-nullable integer is 0. See default Values in C#
Mamoo's reference to nullable types is also worth referring to. Your unit test should therefor test against 0 for a failure or greater than 0 for a pass.