我在使用 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/