0

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); } } } 
3
  • Where does the Insert method come from? Entity Framework? How do you map the data type to a table? Commented Dec 30, 2023 at 19:17
  • I have edited the question with more information Commented Dec 30, 2023 at 19:22
  • 4
    You could use a base class and derived classes, so the derived classes have different Table attributes, 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 your BoxingTimerDatabase class is disposing the connection when it is itself disposed. Commented Dec 30, 2023 at 19:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.