c# - npoco.Insert<T> 在带有 OUT AutoIncrement 主键的表上失败

标签 c# mysql orm petapoco npoco

我在使用 nPoco 的“T 插入”方法时遇到问题,MySQL 表定义如下:

CREATE TABLE `TestTable` (
    `Id` INT(11) NOT NULL,
    `StatusEnum` INT(11) NOT NULL,
    PRIMARY KEY (`Id`)
)

Database.Entity 定义为...

[TableName("operations.TestTable")]
[PrimaryKey("Id")]
[ExplicitColumns]
public class TestTable
{
    [Column]
    public int Id { get; set; }

    [Column]
    public Status StatusEnum { get; set; } = Status.Default;
}

代码片段:

// this works   
var foo = new TestTable { Id = 123 };
database.Execute(
    "insert into operations.TestTable (Id, StatusEnum) values (@0, @1)", 
    foo.Id, 
    foo.StatusEnum);

// this throws "Field 'Id' doesn't have a default value"
var foo2 = new TestTable { Id = 456 };
database.Insert<TestTable>(foo2);   

查看 nPoco 为两个插入生成的 SQL,我看到了这一点(从我的日志文件中)...

SQL: insert into operations.TestTable (Id, StatusEnum) values (?0, ?1) -> ?0 [Int32] = "123" -> ?1 [Int32] = "1"
SQL: INSERT INTO operations.TestTable (`StatusEnum`) VALUES (?0); SELECT @@IDENTITY AS NewID; -> ?0 [Int32] = "1"

对于'Insert of T'方法,为什么nPoco认为Id列是标识列?从类定义中删除“PrimaryKey”属性没有帮助。

注意:此问题与 this question 类似,但可能略有不同。

最佳答案

尝试将您的主键定义更改为

[PrimaryKey("Id", AutoIncrement=false)]

关于c# - npoco.Insert<T> 在带有 OUT AutoIncrement 主键的表上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39728192/

相关文章:

c# - 如何使用 P/Invoke 在 C# 中返回列表?

mysql - 使用内连接从多个表中获取列数据

MySQL 选择 : Case When Or Something Else?

java - orphanRemoval 在 hibernate 状态下无法正常工作 : Element inserted and removed from list is persisted in DB

c# - 使用 OAuth 连接到 Windows Azure Active Directory

c# - WebAuthenticationBroker.AuthenticateAsync 不工作

c# - 如何查看HTTPRequest信息

mysql - 同一张表上的 SQL 三重连接很慢

java - 如何在 mybatis 的结果映射中返回具有空值的列

php - 约定表名(带下划线)