.net使用EF操作SQLite (codefirst vs2015)

配置config


连接字符串
  <connectionStrings>
    <add name="sqliteconn" connectionString="data source=sqLiteTestDB.db" providerName="System.Data.SQLite.EF6"/>
  </connectionStrings>

支撑程序


   <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>

编写model类


    using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace safetodelete_efSqLite_ConsoleApplication1
{
    [Table("employee")]
    class Employee
    {
        [Key]
        public String employeeId { get; set; }
        public String name { get; set; }
    }
}

编写context类


    using System.Data.Entity;
using System.Data.Common;
using System.Data.SQLite.EF6;
using SQLite.CodeFirst;

namespace safetodelete_efSqLite_ConsoleApplication1
{
    class EmployeeContext : DbContext
    {
        public EmployeeContext() : base("sqliteconn") { }

        public EmployeeContext(DbConnection sqliteCon) : base("sqliteconn")
        {
            this.sqliteCon = sqliteCon;
        }
        public DbSet<Employee> employees { get; set; }
        static string dbPath = @"data source=sqLiteTestDB.db";
        private DbConnection sqliteCon;
        public static EmployeeContext Instance
        {
            get
            {
                DbConnection sqliteCon = SQLiteProviderFactory.Instance.CreateConnection();
                sqliteCon.ConnectionString = dbPath;
                return new EmployeeContext(sqliteCon);
            }
        }      
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //如果不存在数据库,则创建
            Database.SetInitializer(new SqliteCreateDatabaseIfNotExists<EmployeeContext>(modelBuilder));
        }
    }
}    

测试程序


           static void Main(string[] args)
        {

            Employee e1 = new Employee();
            e1.employeeId = Guid.NewGuid().ToString();
            e1.name = "xiaoming";

            EmployeeContext ec = new EmployeeContext();
            ec.employees.Add(e1);
            ec.SaveChanges();     

            Console.WriteLine("hello world");
            Console.ReadKey();            

        }

 

2019-03-28

sqlite只能在一个Context类中创建、更新数据库,也就是说如果一个项目中存在多个Context和Model类时codefirst也只能创建或者更新一个Context以及model;

解决方法:

 
class EmployeeContext : DbContext
    {
        public EmployeeContext() : base("employeedbconn") { }
        public DbSet<Employee> employees { get; set; }
        public DbSet<CheckRecord> checkRecords { get; set; }


        public EmployeeContext(DbConnection sqliteCon) : base("employeedbconn")
        {
            this.sqliteCon = sqliteCon;
        }
        static string dbPath = @"data source=employeedb.db";
        private DbConnection sqliteCon;
        public static EmployeeContext Instance
        {
            get
            {
                DbConnection sqliteCon = SQLiteProviderFactory.Instance.CreateConnection();
                sqliteCon.ConnectionString = dbPath;
                return new EmployeeContext(sqliteCon);
            }
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //如果不存在数据库,则创建
           
            

      Database.SetInitializer(new SqliteCreateDatabaseIfNotExists<EmployeeContext>(modelBuilder));
       //     Database.SetInitializer(new SqliteCreateDatabaseIfNotExists<CheckRecordContext>(modelBuilder));

        }


    }

 

如上所示:

可以在一个Context类中包含其它需要CodeFirst的Model类。这样就可以在创建或者初始化开始时一并创建所有Model。

照此机制创建一个专门用来创建和更新表结构的生成类Context把需要的Model全都放里,这样其它的Context类就不用在进行更新表结构的操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值