I'm new to using Tables with Linq in classes in C#, So forgive me if my terminology is incorrect! Also relatively new to Databases in general, so let me know if this is a basic Database concept I have overlooked.
I have a "person" class similar to the one below and I've defined a column called Id which is the primary key - i.e. it has to be unique.
I also specified isDbGenerated = true so that I don't have to manually specify this ID.
[Table]
public class Person
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int Id;
...
}
This works great when I add rows to my table.
When I delete rows from my table, it leaves gaps in the sequence of row IDs. This is fine.
The problem arises when I try saving out my data and re-loading it into the table. To save data, I manually write out the information I need to a text file.
When I re-load the data into my table, the automatically generated ID overrides the original ID I had saved the information out with. This causes issues with my larger database which refers to these rows by their IDs.
For example.
My database contains These rows:
- Id, Name
- 1, John
- 2, Jim
- 4, Mark (note #3 is missing because I deleted it at some point)
- 5, David
I save these out to a text file. I load them back into an empty table from the text file at another time.
- 1, John
- 2, Jim
- 3, Mark
- 4, David
The IsDBGenerated feature causes these numbers to be sequential.
What is the best way for me to set this up so that I can re-load data into the table while retaining the unique IDs?
EDIT: I figured that using an auto generated key isn't making my life easier.
What is the best way to self-generate the next primary key to use? a. Just increment the number after the last entry? b. generate a GUID? c. existing function?