我在使用 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"
对于“T 的插入”方法,为什么 nPoco 认为 Id 列是标识列?从类定义中删除“PrimaryKey”属性没有帮助。
注意:此问题与此问题类似,但可能略有不同。