0

I'm using SQLite and Entity Framework code-first. And my Windows desktop application needs the ability to Create and Open files.

For creating a database, I found DbContext.Database.Migrate(), which will create the database if needed, and them make the schema current.

For opening a database, I'm not as certain. I may try to automatically updated it, but I might also prompt the user before doing so.

I found DbContext.Database.GetMigrations(), but how would I use that to determine if the database file is already current?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I'm not a big user of EF but what would be the point of letting the user choose not to update the DB? Then you just have inconsistent state between the application models and the database and basically the application will be broken. – topsail Aug 01 '22 at 15:27
  • @topsail: Reason 1: There is always some risk in updating a file that it could fail. Reason 2: If the file is updated, then it may not be usable on an earlier version of the software. That's a choice for the user. – Jonathan Wood Aug 01 '22 at 15:37
  • But if the file is not updated it is definitely not usable on the current version of the software. That's the point of migrations, isn't it? To support changes in the application? Or why else would you be migrating anything? Unless your migrations are always nothing more than optional goodies that aren't really necessary. – topsail Aug 01 '22 at 18:44
  • @topsail: Again, they may need it to run on an older version of the software. This happens all the time. For example, Visual Studio warns you before converting projects. I don't know why you think you should be making all these decisions without input from the user, but that's not the direction I'm going. – Jonathan Wood Aug 02 '22 at 02:02

1 Answers1

0

I also found DbContext.Database.GetAppliedMigrations() and DbContext.Database.GetPendingMigrations().

DbContext.Database.GetPendingMigrations() can be used to get all the migrations that are not applied to the database.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466