【发布时间】:2017-02-05 07:03:07
【问题描述】:
我在使用 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;
}
代码sn-p:
// 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”属性没有帮助。
注意:此问题与this question 类似,但可能略有不同。
【问题讨论】:
-
尝试将主键定义更改为 [PrimaryKey("Id", AutoIncrement=false)]
-
就是这样!添加解决方案,我会标记它。我不知道为什么我没有想到这一点。虽然默认为“真”似乎很奇怪
标签: c# mysql orm petapoco npoco