简体   繁体   English

如何在C#中使用Entity Framework使用字符串值生成自动增量编号

[英]How generate auto increment no with string value using Entity Framework in C#

Is it possible to auto generate entity value using Entity Framework? 是否可以使用Entity Framework自动生成实体值? For example a registration id such as 2015ACAD0001 - in this value, the first 4 digits show the year, the next 3 digits show any prefix according to requirement, and the remaining digits are auto increment no as per specific year and prefix 例如,注册ID(例如2015ACAD0001 -在此值中,前4位数字显示年份,后3位数字显示根据要求的任何前缀,其余数字为根据特定年份和前缀的自动递增编号

Yes, with a computed column: 是的,带有计算列:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Foo>()
        .Property(p => p.RegistrationId)
        .HasComputedColumnSql([SQL HERE]);
}

How to calculate the exact id is on you. 如何计算准确的ID在您身上。 Generally-speaking, it just needs to be a SQL expression that evaluates to the type of the column you're computing for, in this case: a string. 通常来说,它只需要是一个SQL表达式即可计算出要计算的列的类型,在这种情况下为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM