我有一个问题,使用nPoco的'Insert of 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类似,但可能略有不同。
答案 0 :(得分:7)
尝试将主键定义更改为
[PrimaryKey("Id", AutoIncrement=false)]