[英]Android Room migration with Dagger 2
我正在使用 Android Room 2.3.0 和 Dagger 2。
提供數據庫實例的DBModule.kt
如下所示:
@Singleton
@Provides
open fun provideDatabase(context: Context): AppDatabase {
return Room.databaseBuilder<AppDatabase>(
context.applicationContext, AppDatabase::class.java,
DATABASE_NAME
).fallbackToDestructiveMigration().build()
}
AppDatabase.kt
class:
@Database(
entities = [User::class],
version = 1,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
現在我需要在用戶實體中添加一些新列並增加數據庫版本。 如果我無法從AppDatabase.kt
訪問 Room.databaseBuilder,如何在AppDatabase.kt
中進行遷移並調用.addMigrations()
?
只需在調用 .build .build()
之前將遷移添加到DBModule.kt
class 即可。
不過要小心.fallbackToDestructiveMigration()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.