【发布时间】:2020-01-20 09:41:44
【问题描述】:
这是我的实体:
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;
});
我的要求:
Audit.Pk:string
PK列:BigInt和AutoIncrement
添加迁移我得到以下错误:
标识值生成不能用于属性 'Pk' on 实体类型“审计”,因为属性类型是“字符串”。身份 值生成只能与有符号整数属性一起使用。
有什么解决方法吗?
【问题讨论】:
-
我是否正确理解您想使用一列作为主键和外键?
-
数据库如何自动增加
string? -
DB column of type bigint not string 那是因为我使用转换器
-
错误消息是关于 property 类型,而不是 db 列类型。考虑一下当前的 EF Core 限制/缺点 - 值生成器和值转换器不能一起使用。
标签: c# .net .net-core entity-framework-core