Currently when I insert a new object in my database, it goes to the same table based on the data type.
For example:
sqlDatabase.Insert(myObject); // -> goes to MyObject table. Is it possible to have several table using the same data type?
For example
sqlDatabase.Insert(myObject, "table1"); sqlDatabase.Insert(myObject, "table2"); Thanks for your help
EDIT:
I am using the sqlite-net package in a .NET 8 MAUI application: https://github.com/praeclarum/sqlite-net
Here is the full code of the class where I use the database:
public sealed class BoxingTimerDatabase { private SQLiteAsyncConnection _sqlDatabase; public BoxingTimerDatabase() { } async Task Init() { if (_sqlDatabase is not null) { return; } _sqlDatabase = new SQLiteAsyncConnection(AppConstants.DatabasePath, AppConstants.Flags); await _sqlDatabase.CreateTableAsync<WorkoutTimer>(); await _sqlDatabase.CreateTableAsync<WorkoutLog>(); } public async Task<List<WorkoutTimer>> GetTimersAsync() { await Init(); return await _sqlDatabase.Table<WorkoutTimer>().ToListAsync(); } public async Task<List<WorkoutLog>> GetWorkoutLogs() { await Init(); return await _sqlDatabase.Table<WorkoutLog>().ToListAsync(); } public async Task<int> LogWorkoutAsync(WorkoutLog workoutLog) { await Init(); return await _sqlDatabase.InsertAsync(workoutLog); } public async Task<WorkoutTimer> GetTimerAsync(int id) { await Init(); return await _sqlDatabase.Table<WorkoutTimer>().Where(i => i.ID == id).FirstOrDefaultAsync(); } public async Task<int> SaveTimerAsync(WorkoutTimer item) { await Init(); if (item.ID != 0) { return await _sqlDatabase.UpdateAsync(item); } else { return await _sqlDatabase.InsertAsync(item); } } }
Tableattributes, but can still be accessed via the base class. Why you would need identical tables is another question. Side note: you need to make sure that yourBoxingTimerDatabaseclass is disposing the connection when it is itself disposed.