问题
I have a WinForms app that I was writing as a side-project. It made use of a local SQL Server database, via Entity Framework, and it worked, but then it was very difficult to create a reasonable installer. It was suggested here that I might consider switching to SQLite, as a more appropriate solution.
To attempt this, I first followed a suggestion I saw elsewhere to download SQLite Database Browser. I used that to create a new database, following the structure of the SQL Server database I already had.
Next, I created a new WinForms project, and added the NuGet package for Microsoft.EntityFrameworkCore.Sqlite.
Then, in order to reverse-engineer my existing database and create the database model for my project, I ran the following commands from the Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.Tools
Update-Package Microsoft.EntityFrameworkCore.Tools
Scaffold-DbContext 'Data Source=C:\Users\Me\Documents\MyApp\MyAppData.db' Microsoft.EntityFrameworkCore.Sqlite
This seemed to succeed (after correcting an error in my database), so I went on to begin adding code which would reference these new database model classes. It was quite a chore, because:
- SQLite INTEGER fields are interpreted as long integers by the EF reverse-engineering tool; whereas, SQL Server INTEGER fields are interpreted as regular integers by the EF reverse-engineering tool.
- SQLite does not seem to support a DATE type for database fields.
So I had a lot of code changes to make.
Once I finished my first wave of code changes, I tried compiling and running MyApp. But there was an error when I tried to add the second record to a table, because the primary key was not unique. (IIRC, for SQL Server Management Studio, marking a field as the PRIMARY KEY would automatically mark it as NOT NULL, UNIQUE and AUTOINCREMENT.)
I removed all the code files which were created by the reverse-engineering, then went back to SQLite Database Browser to mark each PRIMARY KEY as NOT NULL, UNIQUE and AUTOINCREMENT. After that, when I tried to run the reverse-engineering again, it simply fails with no error message other than:
Build failed.
I removed the AUTOINCREMENT tag from the PRIMARY KEY fields in my database and tried again, but got the same result.
What do I need to do to get this reverse-engineering command to work?
来源:https://stackoverflow.com/questions/62336910/converting-code-from-sql-server-to-sqlite-using-reverse-engineering