【问题标题】:sqlite-net-extensions - InsertWithChildrenAsyncsqlite-net-extensions - InsertWithChildrenAsync
【发布时间】:2018-09-26 01:06:27
【问题描述】:

我正在尝试使用“sqlite-net-extensions”创建一个表。但问题总是返回错误。

返回的错误是“只读”。

我创建了两个模型来创建表格。

模型 --> 登录

[PrimaryKey, AutoIncrement, Column("login_id")]
    [Indexed(Name = "LoginId", Order = 2, Unique = true)]
    public int Id { get; set; }

    [NotNull, Column("login_user")]
    [Indexed(Name = "LoginId", Order = 1, Unique = true)]
    public string UserName { get; set; }

    [NotNull, Column("login_password")] public string Password { get; set; }

    [OneToOne("login_id", "Login")]
    public User User { get; set; }

模型 --> 用户

[PrimaryKey, AutoIncrement, Column("user_id")]
    public int Id { get; set; }

    [NotNull, Column("user_name")]
    [Indexed(Name = "UserId", Order = 3, Unique = true)]
    public string Name { get; set; }

    [NotNull, Column("user_email")]
    [Indexed(Name = "UserId", Order = 2, Unique = true)]
    public string Email { get; set; }

    [ForeignKey(typeof(Login)), NotNull, Column("login_id")]
    [Indexed(Name = "UserId", Order = 1, Unique = true)]
    public int LoginId { get; set; }

    [OneToOne("login_id", "User", CascadeOperations = CascadeOperation.All, ReadOnly = false)]
    public Login Login { get; set; }

插入方法

public async Task InsertWithChildren(object o)
    {
        var connection = _sqliteWrapper.OpenDatabase();
        await connection.InsertWithChildrenAsync(o, recursive: true);
    }

【问题讨论】:

  • 您的数据库在哪里?如果它包含在您的应用程序包中,那么它将不可写。您需要先将其复制到用户文件夹。
  • 我同意 Jason 的假设。如果数据库与应用程序一起部署,它将位于只读位置。看看Deploying a database file with Xamarin.Forms app

标签: c# sqlite xamarin sqlite-net-extensions


【解决方案1】:

按照接下来的步骤进行

1.- 创建 SQLite 连接。

2.- 创建您的表格

3.- 插入数据库

 var connection = new SQLiteAsyncConnection(YourDBPath);
 connection.CreateTableAsync<Login>().Wait();
 connection.CreateTableAsync<User>().Wait();
 connection.InsertWithChildrenAsync(YourItem);

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多