Sqlite For C# EF

Sqlite EF

1.創建控制檯項目Lession1
2.安裝Sqlite

install-package system.data.sqlite
install-package sqlite.Codefirst   //Code First 用於生成數據庫表結構,否則不會自動生成表結構

image

3.修改配置文件 app.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.8" />
  </startup>
  <entityFramework>
    <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" />
	  <!--增加下面這行(複製上面一行,去掉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="MyConnection" connectionString="Data Source=.\mydb.db" providerName="System.Data.SQLite.EF6"/>
	</connectionStrings>
</configuration>

4.建立模型

using System.ComponentModel.DataAnnotations.Schema;

namespace Lesson1
{
    [Table("StudentInfo")]
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

5.建立數據上下文

using SQLite.CodeFirst;
using System.Data.Entity;
namespace Lesson1
{
    public class MyContext:DbContext
    {
        //指定自定義的數據庫連接 MyConnection
        public MyContext() : base("MyConnection") { }
        //重寫如下方法,否則默認用的是SqlServer 的模型創建方法 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var init = new SqliteCreateDatabaseIfNotExists<MyContext>(modelBuilder);
            Database.SetInitializer(init);
        }
        public DbSet<Student> Students { get; set; }
    }
}

6.編寫測試用例

using System;
using System.Collections.Generic;
using System.Linq;

namespace Lesson1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student s1=new Student() { Id=1,Name="test1"};
            Student s2 = new Student() { Id = 2, Name = "test2" };
            List<Student> students = new List<Student>() { new Student { Id=3,Name="test3",
           },new Student{ Id=4,Name="test4"} };
            using(var mydb=new MyContext())
            {
                mydb.Students.AddRange(students);
                mydb.SaveChanges();
                var query = from r in mydb.Students select r;
                foreach (var item in query) {
                    Console.WriteLine($"編號:{item.Id},姓名:{item.Name}");

                }
                Console.ReadLine();
            }
        }
    }
}

image

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章