Reputation: 183
I have a database issue. well im not sure exactly what to do. Im using the codefirst approach in an mvc5 project. Currently my primary keys for my tables are auto generated and they increase. eg. customerid - 0,1,2,3 etc.
I want to know, how do i add a prefix to this id, eg i want it to be Cust0 instead of just 0.
Upvotes: 3
Views: 1411
Reputation: 3217
The question is what is the reason you want to have this kind of "key"? You can auto generate ID for your customer and create your own property:
public class Customer
{
public const string CustomerPrefix = "Cust";
[Key]
public int Id {get; set;}
[NotMapped]
public string CustomerId
{
get { return string.Concat(CustomerPrefix, Id)}
}
}
Btw.: It is really bad practice to have PK as a string (because of performance)
Upvotes: 3