Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF core 3.0标识列和值转换器_C#_.net_.net Core_Entity Framework Core - Fatal编程技术网

C# EF core 3.0标识列和值转换器

C# EF core 3.0标识列和值转换器,c#,.net,.net-core,entity-framework-core,C#,.net,.net Core,Entity Framework Core,这是我的实体: public class Audit { public string Pk { get; set; } public string TableName { get; set; } public string RowPk { get; set; } public string ActionType { get; set; } public string RowContent { get; set; } } 这是我的映射: modelBuil

这是我的实体:

public class Audit
{
    public string Pk { get; set; }
    public string TableName { get; set; }
    public string RowPk { get; set; }
    public string ActionType { get; set; }
    public string RowContent { get; set; }
}
这是我的映射:

modelBuilder.Entity<Audit>(entity =>
{
    entity.HasKey(e => e.Pk).IsClustered(true);
    entity.Property(e => e.Pk)
    .ValueGeneratedOnAdd()
    .UseIdentityColumn()
    .HasConversion(new ValueConverter<string, long>(v => Convert.ToInt64(v),v => Convert.ToString(v)));

    entity.Property(e => e.TableName).IsRequired().HasMaxLength(255);
    entity.Property(e => e.RowPk).IsRequired().HasMaxLength(100);
    entity.Property(e => e.ActionType).IsRequired().HasMaxLength(1);
    entity.Property(e => e.RowContent).IsRequired;
});
modelBuilder.Entity(Entity=>
{
entity.HasKey(e=>e.Pk).IsClustered(true);
entity.Property(e=>e.Pk)
.ValueGeneratedOnAdd()
.UseIdentity列()
.HasConversion(新的值转换器(v=>Convert.ToInt64(v),v=>Convert.ToString(v));
Property(e=>e.TableName).IsRequired().HasMaxLength(255);
Property(e=>e.RowPk).IsRequired().HasMaxLength(100);
entity.Property(e=>e.ActionType).IsRequired().HasMaxLength(1);
Property(e=>e.RowContent).IsRequired;
});
我的要求:

Audit.Pk:
字符串

主键列:
BigInt
AutoIncrement

添加迁移时,会出现以下错误:

标识值生成不能用于上的属性“Pk” 实体类型为“审核”,因为属性类型为“字符串”。身份 值生成只能与有符号整数属性一起使用


有什么解决方法吗?

对于SQL表来说,保留bigint主键更容易维护。否则,将主键
PK
更改为类似GUID的内容

否则,我建议对您的类进行以下更改,即在您的程序中必须使用PkString(此更改对您的数据库是透明的):


就个人而言,
字符串
不是
主键
的常见数据类型。大多数情况下,我们使用
int
GUID
。为了解决您的问题,您可以使用以下代码段:

public class Audit
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int Id { get; set; }  //instead of Pk 

   [NotMapped]
   public string Pk => Id.ToString();


   .  
   .
   .
}

最后,从映射中删除与
Pk
属性相关的所有代码

我是否正确理解您希望使用一个COLLMN作为主键和外键?数据库如何自动增加
字符串
?类型为bigint not string的DB列这是因为我使用了转换器错误消息谈到属性类型,不是db列类型。考虑它当前的EF核心限制/缺点-值发生器和值转换器不能一起使用。
public class Audit
{
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int Id { get; set; }  //instead of Pk 

   [NotMapped]
   public string Pk => Id.ToString();


   .  
   .
   .
}