EF核心:如何:使用特定规则的数据库生成的字符串? [英] EF Core: How to: Database generated string with specific rules?

查看:69
本文介绍了EF核心:如何:使用特定规则的数据库生成的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用流利的api创建具有特定规则的数据库生成的字符串?

Is it possible with the fluent api to create a database generated string with specific rules?

比如说:字符串应以 a开头,然后是一个数字,递增1,最小起始值为10000。

Like say: The string should start with "a" then a number that is incremented by 1 and minimum starting value is 10000.

例如a10001,a10002,...

e.g. a10001, a10002,...

推荐答案

您可以使用 HasSequence HasDefaultValueSql

示例在SQL Server中有效,不确定其他提供程序。

Example works in SQL Server, not sure about other providers.

public class Foo
{
    public int FooId { get; set; }
    public string GeneratedString { get; set; }
}

public class FooContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasSequence<int>("GeneratedStringSequence")
            .StartsAt(10000)
            .IncrementsBy(1);

        modelBuilder
            .Entity<Foo>()
            .Property(f => f.GeneratedString)
            .HasDefaultValueSql("FORMAT((NEXT VALUE FOR GeneratedStringSequence), 'a#')");
    }
}

这篇关于EF核心:如何:使用特定规则的数据库生成的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆