我同意这是一个非常奇怪和令人困惑的行为,因为他们的 API 并不总是使用该属性。
有两种方法可以使它工作。
一种是不使用示例中的属性,而是使用带有 autoIncrement 参数的重载方法,并将其设置为false。这是一个完整的例子:
// Create a PetaPoco database object
var db = new PetaPoco.Database("ConnectionSt");
// Create an article
var a = new Article
{
article_id = 152,
title = "My new article",
content = "PetaPoco was here",
date_created = DateTime.UtcNow
};
// Insert it by turning off auto-increment
db.Insert("articles", "article_id", false, a);
另一种是使用以对象为参数的插入方法:
// Create an article
var a = new Articles
{
article_id = 1111,
title = "My new article",
content = "PetaPoco was here",
date_created = DateTime.UtcNow
};
// Insert it by using the insert method that will use the attribute!
db.Insert(a);
将对象作为参数的重载是唯一在PrimaryKey内部使用该属性的重载,它在您的场景中应该像魅力一样工作。