当前,当我在数据库中插入新对象时,它会根据数据类型转到同一个表。
例如:
sqlDatabase.Insert(myObject); // -> goes to MyObject table.
是否可以有多个表使用相同的数据类型?
例如
sqlDatabase.Insert(myObject, "table1");
sqlDatabase.Insert(myObject, "table2");
感谢您的帮助
编辑:
我在 .net8 MAUI 应用程序中使用 sqlite-net 包:https://github.com/praeclarum/sqlite-net
这是我使用数据库的类的完整代码:
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);
}
}
}