开发者

Not getting key value from Identity column back after inserting new row with SubSonic ActiveRecord

开发者 https://www.devze.com 2022-12-31 19:51 出处:网络
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

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--


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:

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消