Basically I have a product entity
public class Product
{
public int ID {get; set;}
public string Name {get; set;}
}
And I want to create :
UnknownProduct : Product
{
public UnknownProduct()
{
ID = -2;
Name = Unknown;
}
}
The objective here is to easily create UnknownProduct from the business logic. I want to have this in the resulting table :
#Products
ID | Name
1 | Beer // Referenced X times
2 | Soda // Referenced Y times
-1 | UnknownProduct // Referenced Z times, so only one row has been create !
However I know that with the previous code, whenever I will create an instance a new row will be added to the table. Actually with the ID = - 2, I'll get a Identity Insert expcetion, but if I remove it, a new row will be added every time.
I've done a similar thing but the primary key was of type string, therefore I had no problem to seed the first instance with an explicit primary key, and then instantiate the following objects with the same primary key, thus avoiding duplicates.
Here, since the PK is of type int, this column is marked as auto-generated, and this is indeed what I want. But in this specific case, I'd like to be able to use UnknownProduct as a referential data, that I can instantiate directly from C# without having duplicates in the DB. Any idea of how I can achieve this ?