Connect Entity Framework with Sqllite database
10:24This walkthrough will get you started with an application that uses the Entity Framework (EF) to read and write data from a SQLite database. It is intended to be similar to the Code First to a New Database walkthrough.
There are currently two SQLite providers for EF that I know of: System.Data.SQLite andDevart's dotConnect for SQLite. Devart's provider has a much richer set of features, but it is also a commercial product. In the spirit of FOSS, we will be using the System.Data.SQLite provider for this walkthrough. I encourage you to keep the Devart provider in mind, however, if your project requires that extra level of support.
Create the Application
For simplicity, we will be using a Console Application, but the basic steps are the same regardless of project type.- Open Visual Studio
- Select File -> New -> Project...
- Select Console Application
- Name the project
- Click OK
Create the Model
For our model, we'll be borrowing pieces from the Chinook Database (a cross-platform, sample database). Specifically, we will be using Artists and Albums.Add the following two classes to your project.
public class Artist
{
public Artist()
{
Albums = new List();
}
public long ArtistId { get; set; }
public string Name { get; set; }
public virtual ICollection Albums { get; set; }
}
public class Album
{
public long AlbumId { get; set; }
public string Title { get; set; }
public long ArtistId { get; set; }
public virtual Artist Artist { get; set; }
}
Create a Context
In EF, the context becomes your main entry point into the database. Before we define our context though, we will need to install Entity Framework.- Select Tools -> Library Package Manager -> Package Manager Console
- Inside the Package Manager Console (PMC) run Install-Package EntityFramework
class ChinookContext : DbContext
{
public DbSet Artists { get; set; }
public DbSet Albums { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Chinook Database does not pluralize table names
modelBuilder.Conventions
.Remove();
}
}
Install the Provider
In order to connect to SQLite databases, we will need to install an appropriate ADO.NET and Entity Framework provider. Luckily, the provider we're using is available via NuGet.- Inside PMC, run Install-Package System.Data.SQLite.x86
Add the Database
Unfortunately, System.Data.SQLite does not support creating databases. So instead of letting Code First create our database, we will need to manually add a database to our project. It's a good thing we're using a sample database that's available for SQLite!- Download and extract the SQLite version of the Chinook Database
- Select Project -> Add Existing Item...
- Browse to the folder where you extracted the database
- Select All Files from the dropdown next to File name
- Select the Chinook_Sqlite_AutoIncrementPKs.sqlite file
- Click Add
- Select the file in Solution Explorer
- In Properties, set Copy to Output Directory to Copy if newer
Start Coding
Ok, we should be ready to start coding our application. Let's see what artists exist in the database. Inside Program.cs, add the following to Main.using (var context = new ChinookContext())
{
var artists = from a in context.Artists
where a.Name.StartsWith("A")
orderby a.Name
select a;
foreach (var artist in artists)
{
Console.WriteLine(artist.Name);
}
}
Hmm, it looks like one of my favorite bands is missing. Let's add it. using (var context = new ChinookContext())
{
context.Artists.Add(
new Artist
{
Name = "Anberlin",
Albums =
{
new Album { Title = "Cities" },
new Album { Title = "New Surrender" }
}
});
context.SaveChanges();
}

0 comments: