sqlite-net-extensions - InsertWithChildrenAsync

808 views Asked by At

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);
    }
1

There are 1 answers

0
Ricardo Romo On

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);