I'm using ASP.NET MVC with Entity Framework Code First and SQL Server 2014 database.
I want to create a 'Customer' table with an autoincrement primary key which is formatted like this : CLI-X where X is the autoincrement number.
Here is what I tried :
public class Customer
{
public int CustomerId { get; set; }
public string CustomerNumber
{
get
{
return "CLI-" + CustomerId;
}
set { }
}
public string Name { get; set; }
}
public class CustomerMapping : EntityTypeConfiguration<Customer>
{
public CustomerMapping()
{
// Auto incrément ID
HasKey(x => x.CustomerId);
Property(x => x.CustomerId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Set CLI-ID as PK
Property(x => x.CustomerNumber).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}
Unfortunately, the column 'CustomerNumber' in is empty in the database when I create a new customer. I'd like this column to be formatted automatically instead of creating it myself each time I create a new customer.
So my question is : How can I do in EF code-first to have a autoincrement primary key with a string prefix inside?