In this tutorial you will learn how to create an application which will use the following technologies:
Note that SQLite full-text search is supported in Entity Framework v4 - v6. It is not supported in Entity Framework Core.
Follow the link at the end of this article to download the complete application sample.
Add EF Code-First NuGet package to the project (note that Entity Framework Core does not support full-text search, so you need to use Entity Framework v6): run Package Manager Console via the Visual Studio Tools menu -> Library Package Manager -> Package Manager Console, and in the displayed window run the following command:
Install-Package EntityFramework
As a result, the EntityFramework.dll assembly is added to the project, App.config is supplemented with records and the packages.config file is created.
Add a new MyContext.cs file to the project, and place an empty model in it:
Run the following command in Package Manager Console:
Enable-Migrations
As a result, the Configuration class is added to the project.
Complement the existing Configuration class constructor
by specifying the SQL generator:
<connectionStrings> <add name="MyContext" connectionString="Data Source=d:\Test.db;FailIfMissing=false" providerName="Devart.Data.SQLite" /> </connectionStrings>
Add the Book.cs file to the project:
Here we use the rowid column, which is auto-generated by SQLite for all the tables, to map the entity key. The Fts property is created for performing full-text search. It is mapped to the virtual column, having the same name as the table. For more information on these properties refer to Full-Text Search Support in Entity Framework.
Add the following line to the MyContext class:
To generate migration, run the following command in Package Manager Console:
Add-Migration AddBook
As a result, a migration class with the following content is added to the project:
Complement the migration by specifying the storage engine type for the table, and creating a full-text index on the Content column, without specifying explicitly the name of the generated index:
Update the database using the migration, by running the following command in Package Manager Console:
Update-Database -Verbose
As a result, commands for model objects creation will be executed
CREATE VIRTUAL TABLE Books USING fts4(Author, Name, Content, tokenize=porter)
Also, commands for creating the system MigrationHistory table and filling it with migration data will be executed.
In this tutorial, we have turned on SQLiteMonitor to view executed DDL and DML statements in the Devart dbMonitor application. However, keep in mind that the use of SQLiteMonitor is feasible for the purpose of testing and debugging but should be limited in production environments, since monitoring can decrease the performance of your application. To use monitoring, add this code to your application
and run dbMonitor.
Let's write the code filling the table with data and forming full-text search queries.