gpt4 book ai didi

mysql - Code First 带前缀的主键自动递增

转载 作者:行者123 更新时间:2023-11-29 06:23:28 24 4
gpt4 key购买 nike

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

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

最佳答案

问题是你想要拥有这种“ key ”的原因是什么?您可以为您的客户自动生成 ID 并创建您自己的属性(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)}
}
}

顺便说一句:将 PK 作为字符串(因为性能)确实是不好的做法

关于mysql - Code First 带前缀的主键自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com