gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:57:19 25 4
gpt4 key购买 nike

我有以下 MVC 5 模型:

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

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

//others

}

表上的id属性是一个主键自增

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

这个更新有效:

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; }

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

关于mysql - 使用自动增量键插入时出现 ORMLite 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com