Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# npoco.插入<;T>;在没有自动递增主键的表上失败_C#_Mysql_Orm_Petapoco_Npoco - Fatal编程技术网

C# npoco.插入<;T>;在没有自动递增主键的表上失败

C# npoco.插入<;T>;在没有自动递增主键的表上失败,c#,mysql,orm,petapoco,npoco,C#,Mysql,Orm,Petapoco,Npoco,我在使用Npco的“Insert of T”方法和定义如下的MySQL表时遇到了一个问题: CREATE TABLE `TestTable` ( `Id` INT(11) NOT NULL, `StatusEnum` INT(11) NOT NULL, PRIMARY KEY (`Id`) ) 数据库。实体定义为 [TableName("operations.TestTable")] [PrimaryKey("Id")] [ExplicitColumns] public

我在使用Npco的“Insert of T”方法和定义如下的MySQL表时遇到了一个问题:

CREATE TABLE `TestTable` (
    `Id` INT(11) NOT NULL,
    `StatusEnum` INT(11) NOT NULL,
    PRIMARY KEY (`Id`)
)
数据库。实体定义为

[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);   
对于“Insert of T”方法,为什么nPoco认为Id列是标识列?从类定义中删除“PrimaryKey”属性没有帮助


注意:此问题与类似,但可能略有不同。

尝试将主键定义更改为

[PrimaryKey("Id", AutoIncrement=false)]

尝试将主键定义更改为[PrimaryKey(“Id”,AutoIncrement=false)],就是这样!添加解决方案,我会标记它。我不知道为什么我没有想到这一点。尽管默认值是“真的”似乎很奇怪
[PrimaryKey("Id", AutoIncrement=false)]