2
我使用了MySQL表nPoco的方法「T的插入」的問題等被定義:定義爲npoco.Insert <T>表上沒有失敗自動增量主鍵
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在t生成的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」,自動遞增= FALSE)] – JAZ
這就是它!添加解決方案,我會標記它。我不知道爲什麼我沒有想到這一點。雖然默認情況下確實是'真' – Tony