使用 SubSonic ActiveRecord 插入新行后未从 Identity 列获取键值

发布于 2024-09-02 14:53:33 字数 686 浏览 2 评论 0原文

我确信我在这里错过了明显的答案,但可以帮忙。我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤寂小茶 2024-09-09 14:53:33

对于你的问题的第一部分无法帮助你太多。也许您应该为此创建一个 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.

£噩梦荏苒 2024-09-09 14:53:33

这对我有用,我使用完全相同的设置:

Account account = new Account();
account.EmailAddress = emailAddress;
account.Identity = identifier;
account.IsActive = true;
account.Save();

int accountId = account.AccountId; // AccountId being an identity column.

本例中没有错误处理,但你明白我的意思了。

This works for me, I'm using exactly the same setup:

Account account = new Account();
account.EmailAddress = emailAddress;
account.Identity = identifier;
account.IsActive = true;
account.Save();

int accountId = account.AccountId; // AccountId being an identity column.

No error handling in this example, but you get my drift.

紫竹語嫣☆ 2024-09-09 14:53:33

除了我之前的回答;整数值不能为空。它是一个整数,因为数据库列是主键,因此根据定义它不应为空。不可为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文