1. Build a solution (framework4.0 is used in this illustration) as shown in the figure:
2, add System.Data.SQLite reference as shown:
3. Make sqlite database files
p>
uses navcat to create an Employee table
4. Name the newly created database file: TestSQLite
and copy it to the bin/Debug of the program, As shown:
5. Configure the link string
"SQLiteContext" connectionString="Data Source=.\TestSQLite " providerName="System. Data.SQLite.EF6" />
6. Establish context
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using System.Text;
namespace ConsoleApp8
{
public class SQLiteTest
{
///
/// This attribute is used to specify the configuration class
///
//[DbConfigurationType(typeof(MyConfiguration))]
public class SQLiteContext: DbContext
{
///
/// Specify the connection string
///
///
public SQLiteContext(string filename)
: base(new SQLiteConnection()
{
ConnectionString =
new SQLiteConnectionStringBuilder()
{DataSource = filename, ForeignKeys = true}
.ConnectionString
}, true)
{
}
///
/// For database generation, you can comment out if you don’t need to generate it
///
///
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
public DbSetEmployees {get; < span style="color: #0000ff;">set;}
}
[Table("Employee")]
public class Employee
{
public int EmployeeID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
}
}
7. Test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
SQLiteTest.SQLiteContext context;
context = new SQLiteTest.SQLiteContext("TestSQLite");
var empList = context.Employees.OrderBy(c => c. FirstName).ToList();
}
}
}
Error 1 The error encountered is as follows:
System.InvalidOperationException: "No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http:// span>go.microsoft.com/fwlink/?LinkId=260882 for more information.”
Solution:
Remove The code of app.config is as follows:
Remove the red line and the new app.config content is as follows:
"1.0" encoding="utf-8"?>
"entityFramework" type="System.Data.Entity.Internal.ConfigFile. EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
"System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFrameworkSystem.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework span>">
"mssqllocaldb" />
"System.Data.SqlClient" type="System.Data.Entity. SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
"System.Data.SQLite.EF6" type="System.Data. SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
"System.Data.SQLite.EF6" />
"SQLite Data Provider (Entity Framework 6)< span style="color: #800000;">" invariant="System.Data .SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
Error 2 If you encounter an error that you cannot find “SQLite.Interop.dll”
Solve Method:
Copy these two folders from the path in the nugit package of the current solution, and copy them to bin/debug, as shown in the figure:
Done< /p>
"SQLiteContext" connectionString="Data Source=.\TestSQLite " providerName="System. Data.SQLite.EF6" />
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using System.Text;
namespace ConsoleApp8
{
public class SQLiteTest
{
///
/// This attribute is used to specify the configuration class
///
//[DbConfigurationType(typeof(MyConfiguration))]
public class SQLiteContext: DbContext
{
///
/// Specify the connection string
///
///
public SQLiteContext(string filename)
: base(new SQLiteConnection()
{
ConnectionString =
new SQLiteConnectionStringBuilder()
{DataSource = filename, ForeignKeys = true}
.ConnectionString
}, true)
{
}
///
/// For database generation, you can comment out if you don’t need to generate it
///
///
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
public DbSetEmployees {get; < span style="color: #0000ff;">set;}
}
[Table("Employee")]
public class Employee
{
public int EmployeeID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
SQLiteTest.SQLiteContext context;
context = new SQLiteTest.SQLiteContext("TestSQLite");
var empList = context.Employees.OrderBy(c => c. FirstName).ToList();
}
}
}
System.InvalidOperationException: "No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. Make sure the provider is registered in< /span> the 'entityFramework ' section of the application config file. See http://go.microsoft.com /fwlink/?LinkId=260882 for more information.”
" span>1.0" encoding= "utf-8"?>
"entityFramework" type="System.Data.Entity.Internal.ConfigFile. EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
"System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework span>">
"mssqllocaldb" />
"System.Data.SqlClient" type="System.Data.Entity. SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
"System.Data.SQLite.EF6" type="System.Data. SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
"System.Data.SQLite.EF6" />
"SQLite Data Provider (Entity Framework 6)< span style="color: #800000;">" invariant="System.Data .SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />