0

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?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • SQL Server 2015? You must have special connections. http://sqlserverbuilds.blogspot.com/ – TTeeple Jul 30 '15 at 14:47
  • How come you are using SQL Server 2015? – FutbolFan Jul 30 '15 at 14:48
  • Sorry my bad, i meant 2014, It's my visual studio which is 2015 :) – Armand Charbonnier Jul 30 '15 at 15:03
  • Have you seen the solution here? http://stackoverflow.com/questions/15585330/calculated-column-in-ef-code-first – TTeeple Jul 30 '15 at 15:09
  • @TTeeple yes read similar topics, the thing is even with a private set and all, when i create a new customer, the field is empty. It only work when i get back the data, it will format the field of the entity and get the right formatted ID. But in database, it is empty. Maybe i'm doing it wrong but all my tests had the same result. – Armand Charbonnier Jul 30 '15 at 15:12
  • Why are you trying to both manually set part of the value and automatically set another part? What possible benefit is there from every single row having a silly prefix on the value? The only place that would make any difference is in the front end. If anything create a computed column that can have your prefix and the value from the identity. – Sean Lange Jul 30 '15 at 16:20
  • Well maybe my explaination wasn't good enough, i'm not trying to do something manually at all. I just want all my customers in database to have a primary key formatted like : CLI-001, CLI-002 etc.. The CLI- is fixed, it never changes, only the number has to auto increment. And i need this format because it's a code used by my customer. And it will be more efficient i guess in the database, if the field is already formatted (for search etc...). – Armand Charbonnier Jul 30 '15 at 16:56
  • Computed column in the database is the way to go. I don't understand how it can be "empty in the database" as you say above. – Gert Arnold Jul 31 '15 at 08:37
  • Well i don't know why, maybe because computed column are meant to be calculated by the database when i'm querying a row. All I can say it is that with the code written above, when I insert a Customer to my database using code first (MyDbContext.Insert(customer)), the row is saved in the database, but the field CustomerNumber is NULL. – Armand Charbonnier Aug 01 '15 at 17:06
  • Please show your computed column specification to get this sorted out. – Gert Arnold Aug 04 '15 at 07:53
  • Everything is written in my first post, what else do you want to know ? – Armand Charbonnier Aug 05 '15 at 18:24

0 Answers0