【问题标题】:Insert with AutoIncrement使用自动增量插入
【发布时间】:2016-05-11 10:04:53
【问题描述】:

我正在尝试将一些数据插入到我的 SQLite 数据库中,当我对一条记录执行此操作时,它可以完美运行。但是在循环中我得到一个错误。首先,这里是代码

string dataSource = "Data Source=";
Connection = new SQLiteConnection(dataSource + this.DatabasePath);

var context = new DataContext(Connection);

var users = context.GetTable<User>();


for (int i = 0; i < 2; i++) {
    User tempUser = new User() {
        ID = null,
        EMail = i + "@" + i + ".de",
        Password = "Test1234",
        JoinedDate = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss")
    };

    users.InsertOnSubmit(tempUser);
    context.SubmitChanges();
}

还有用户本身

[Table(Name = "User")]
public class User {

    [Column(Name = "UserID", IsPrimaryKey = true, CanBeNull = false)]
    public int? ID { get; set; }

    [Column(Name = "EMail", CanBeNull = false)]
    public string EMail { get; set; }

    [Column(Name = "Password", CanBeNull = false)]
    public string Password { get; set; }

    [Column(Name = "JoinedDate", CanBeNull = false)]
    public String JoinedDate { get; set; }

    [Column(Name = "PaymentMethodID")]
    public int PaymentMethodID { get; set; }
}

表格是这样创建的

CREATE TABLE "User" (
`UserID`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`EMail` TEXT NOT NULL,
`Password`  TEXT NOT NULL,
`JoinedDate`    TEXT NOT NULL,
`Licenses`  INTEGER,
`PaymentMethodID`   INTEGER
)

最后我得到的错误:

System.Data.Linq.dll 中出现“System.Data.Linq.DuplicateKeyException”类型的异常,但未在用户代码中处理

其他信息:Eine Entität、deren Schlüssel bereits verwendet wird、kann nicht hinzugefügt werden。

我敢打赌,这是因为 ID 字段设置为 AutoIncrement

【问题讨论】:

标签: c# linq sqlite


【解决方案1】:

假设您的代码在一次插入时工作正常,我会尝试其中一种解决方案

解决方案 1

在循环外调用context.SubmitChanges();

所以

for (int i = 0; i < 2; i++) {
    User tempUser = new User() {
        ID = null,
        EMail = i + "@" + i + ".de",
        Password = "Test1234",
        JoinedDate = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss")
    };

    users.InsertOnSubmit(tempUser);

}
context.SubmitChanges();

解决方案 2

使用InsertAllOnSubmit

var tempUsers = Enumerable.Range(0, 2)
                      .Select(i => new User{
                                   ID = null,
                                   EMail = i + "@" + i + ".de",
                                   Password = "Test1234",
                                   JoinedDate = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss")
                      });

users.InsertAllOnSubmit(tempUsers);
context.SubmitChanges();

解决方案 3

在每个循环中处理和重新创建上下文(似乎是一个相当糟糕的主意)

for (int i = 0; i < 2; i++) {
   using (var context = new DataContext(Connection)) {
     var users = context.GetTable<User>();

     User tempUser = new User() {

        ID = null,
        EMail = i + "@" + i + ".de",
        Password = "Test1234",
        JoinedDate = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss")
    };

     users.InsertOnSubmit(tempUser);
     context.SubmitChanges();
   }
}

【讨论】:

  • 只有解决方案 3 对我有用。但这是一个好的解决方案吗?尤其是在牢记速度时
  • @DanielDirtyNativeMartin 不,这不是...另一种方式,也许是:stackoverflow.com/questions/31145955/…
【解决方案2】:

你只需去掉 ID = null,因为这是自增的,不需要写这个。 更改表“用户”删除自动增量列 ID 中的 NOT NULL 检查,并在“用户”类中删除。 ID 列是自动递增的,因此不需要使用 NOT NULL 约束进行检查。

for (int i = 0; i < 2; i++) {
    User tempUser = new User() {
        EMail = i + "@" + i + ".de",
        Password = "Test1234",
        JoinedDate = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss")
    };

【讨论】:

  • 我做了,但还是一样的错误
  • 请删除用户 ID 中的 NOT NULL 检查 => UserID INTEGER PRIMARY KEY AUTOINCREMENT,
  • 您需要从表“用户”和类“用户”中删除
  • 当然,我做到了。我还在UserID上方添加了[Column(Name = "UserID", IsPrimaryKey = true, CanBeNull = true)]
  • 请像这样使用 [Column(Name = "UserID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY")]
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 2012-02-01
相关资源
最近更新 更多