基於VS2015、EF6連線Sqlite,Linq和部分lambda實現資料庫資料的基本增刪改查
阿新 • • 發佈:2018-12-20
首先引用管理Nuget包,將所需要的EntityFramework包以及Sqlite的包下載好,
只需要下載SQlite包,其他的依賴包會自動下載。
之後會自動生成一個app.config配置檔案
但是在原本生成的config檔案中沒有
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
另外資料庫連線語句:這裡data source可以寫絕對路徑,也可以直接寫SqliteTest.db,但此時db檔案必須放在專案的bin資料夾當中。
<connectionStrings>
<add name="SqliteTest" connectionString="Data Source=D:\SqliteTest.db" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
完整的config檔案
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="System.Data.SQLite.EF6" /> <add name="SQLite Data Provider (Entity Framework 6)" 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" /> <remove invariant="System.Data.SQLite" /> <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> </DbProviderFactories> </system.data> <connectionStrings> <add name="SqliteTest" connectionString="Data Source=D:\SqliteTest.db" providerName="System.Data.SQLite.EF6" /> </connectionStrings> </configuration>
config檔案配置好後,在VS中編寫表的對應表名(實體類),實體類表中屬性需要和資料庫中完全一致,在這裡遇到一個小問題,查詢時不區分大小寫,但是增刪改時,嚴格區分。
//特性 -引用名為Person的表名 //[Table("Actor")] //Actor(資料庫的表名) public class Actor { public Int64 ID { get; set; } //注意要用Int64 public string FirstName { get; set; } public string LastName { get; set; } } public class Countries { //資料庫中countries的實體類 public Int64 ID { get; set; } public string Name { get; set; } public Int64 Cost { get; set; } }
這裡需要注意的是,因為在資料庫中表名是以複數形式存在的,比如我們在程式碼的實體類和資料庫的表名都取為Actor,這時雖然資料庫表名顯示的是Actor,但是它預設是以Actors形式存在,所以我們在查詢的時候就會出現異常
兩種解決方案:
1.將資料庫的表名直接改為Actors(複數形式)
2.使用特性,直接將資料庫的表名寫死
編寫上下文
public class TestContext : DbContext
{
//資料上下文,建立Actor的表物件
//資料庫中的表名是以負數形式來進行表現的(比如這裡dbset建表為actor,那麼資料庫中必須為actors才能正確查詢)
public DbSet<Actor> Actors { get; set; }
public DbSet<Countries> Countries { get; set; }
/// <summary>
/// 定義建構函式,繼承自父類的建構函式,通過繼承父類的建構函式來建立資料庫
/// 父類建構函式的引數是配置檔案中配置的資料庫連線字串
/// </summary>
public TestContext(): base("SqliteTest")
{
}
}
資料庫內容
然後連線資料庫,根據自己的資料庫進行選擇和連線。
連線好後,進行增刪改查,直接在主函式呼叫方法就可以了
//查詢內容
public static void select()
{
using (TestContext mc = new TestContext())
{
//var mcmc = mc.Person.Where(p => p.FirstName == "阿達").Select(p => p.LastName);
//linq語句查詢
var linq = from n in mc.Actors
where n.ID == 4
select n.FirstName + " " + n.LastName;
//lambda語句查詢
var lambda = mc.Actors.Where(p => p.ID == 4).Select(p => p.FirstName);
foreach (var item in lambda)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
//新增內容
public static void add()
{
using (TestContext mc = new TestContext())
{
Actor a = new Actor();
a.ID = 6;
a.FirstName = "chen";
a.LastName = "long";
mc.Actors.Add(a);
mc.SaveChanges();
}
Console.WriteLine("add successfully!");
Console.ReadLine();
}
//刪除內容
public static void remove()
{
using (TestContext mc = new TestContext())
{
var result = from n in mc.Actors
where n.ID == 5
select n;
foreach (var item in result)
{
mc.Actors.Remove(item);
}
//必須savechange否則不會成功
mc.SaveChanges();
Console.WriteLine("Delete successfully");
Console.ReadLine();
}
}
//修改內容
public static void modify()
{
using (TestContext mc = new TestContext())
{
//修改指定查詢到的內容
var result = from n in mc.Actors
where n.ID == 4
select n;
foreach (var item in result)
{
item.FirstName = "修改";
}
////修改查詢多條內容中的第一條內容
//var result1 = from n in mc.Actors
// select n;
//var target=result1.FirstOrDefault<Actor>();
//target.LastName = "修改111";
//必須savechange,否則不會成功
mc.SaveChanges();
Console.WriteLine("Modify successfully");
Console.ReadLine();
}
}