如何添加列自动增量,但这不是主键或标识

问题描述 投票:0回答:1

我想在保存行时按国家/地区添加计数器,例如:

表国家

身份证 姓名
1 法国
2 西班牙

表“销售额”-> 按国家/地区记录销售额

身份证 类别 销售额
1 1 1
2 1 2
3 2 1
4 1 3
5 2 2
6 1 4
_context.Sales.Add(register);
_context.SaveChanges()

SaveChanges
之后,
numsales
始终为空。

谢谢!

c# entity-framework-core
1个回答
0
投票

尝试在定义实体模型时添加:

public class MyEntity
{
    public int Id { get; set; } // Primary key

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int NonPrimaryKeyField { get; set; }
    // Other properties...
}

这将配置插入新记录时数据库生成的 NonPrimaryKeyField 属性

© www.soinside.com 2019 - 2024. All rights reserved.