简体   繁体   中英

sqlite-net-extensions - InsertWithChildrenAsync

I'm trying to create a table using the "sqlite-net-extensions". But the problem is always returning with error.

The error that returns is "Read only".

I created two models to create the tables.

Model --> Login

[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; }

Model --> User

[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; }

Insert method

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

Follow the next steps

1.- Create a SQLite connection.

2.- Create your tables

3.- Insert into database

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM