代码优先使用前缀的主键自动增量

时间:2015-08-29 21:26:25

标签: mysql entity-framework ef-code-first code-first

我遇到了数据库问题。我不确定该怎么做。我在mvc5项目中使用codefirst方法。 目前我的表的主键是自动生成的,并且它们会增加。例如。 customerid - 0,1,2,3等

我想知道,如何为此ID添加前缀,例如我希望它是Cust0而不是0。

1 个答案:

答案 0 :(得分:2)

问题是你想要拥有这种关键&#34 ;?的原因是什么?您可以为客户自动生成ID并创建自己的属性:

public class Customer
{
   public const string CustomerPrefix = "Cust";

   [Key]
   public int Id {get; set;}

   [NotMapped]
   public string CustomerId 
   {
      get { return string.Concat(CustomerPrefix, Id)}
   }
}

顺便说一句:将PK作为一个字符串(因为性能)是非常糟糕的做法