This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have a table called jjUsers and I do not wish to use an auto-incrementing identity column but rather a char(10) column named UserId. It is my primary key. The entity class LightSpeed created only appears to have an .Id property whereas the actual column name is UserId. I am using this code and get an "Object not set to an instance of the object" when it tries to set the .Id value to a string value obtained from an onscreen textbox using the .Text.ToUpper() function. I can see in the Watch window that jjUser is in fact an instantiated object All has been going well with other tables using an auto-incrementing identity column but now I am hoping there is a way to make this work. Thanks for your help!
|
|
|
Ok, I found in the online LightSpeed book the section about using natural keys. However, I am lost as to where I need to put the code like the example so I can set the "transient" value. Thanks for an explanation of where to put the code and how to set the value in my routine. |
|
|
I have tried putting in this code using the example in the book but still the the "Object not set to an instance of an object" error - public partial class jjUser : Entity
|
|
|
As you've figured out, the natural key field goes in the partial class. The error may be occurring because the natural key is not set up, i.e. the field is not populated. The preferred technique is to initialise the natural key field in your constructor i.e.
This guarantees that a jjUser can never have a null ID. If that still doesn't help then could you post the stack trace of the exception or better still a minimal buildable repro project (console application or NUnit test) please? Thanks! |
|
|
Thanks that seems to work. Question - by putting this code in the model class, do I have to worry about it being overwritten if I change the model diagram by updating from the database? If so, how does one make such changes in a way that they don't get overwritten? |
|
|
Don't put this code in the generated file, because that will get overwritten. Put it in a partial class file. You can create a partial class file manually, or by right-clicking an entity in the designer and choosing Refactor > Create Partial Class. |
|