使用自动增量键插入时出现 ORMLite 错误

ORMLite error on insert with autoincrement key

我有以下 MVC 5 Model:

[Schema("dbo")]
[Alias("map")]
public class Map {
    [PrimaryKey]
    [Alias("id")]
    public int Id { get; set; }

    [Alias("name")]
    public String Name { get; set; }

    //others

}

table 上的 id 属性是 primary key autoincrement

我需要 Id 来执行更新操作,但这会阻止我插入新条目。

这个update有效:

var res = dbConnection.Update<Map>(map);

虽然这个 insert 没有:

var res = dbConnection.Insert<Map>(map, selectIdentity: true);

我得到这个异常:

Cannot insert explicit valuefor identity column in table 'map' when IDENTITY_INSERT is set to OFF.

我该怎么做才能解决这个问题?

[AutoIncrement] 属性指示 ORMLite 从 INSERT 字段列表中省略属性字段。

所以将它应用到 Id 属性:

[PrimaryKey]
[AutoIncrement]
[Alias("id")]
public int Id { get; set; }

插入或更新该实体时将忽略其值。