经过EntityFramework操做sqlite(DbFirst)

记录一下经过 EntityFramework6 来操做sqlite过程
环境:sql

  • visual studio 2017
  • .net 4.5
  • Console Application(项目类型)
  • sqlite3
  • navicat for sqlite

设计数据库

我使用了 navicat for sqlite 这个图形界面工具来快速生成数据库的;
很是简单,打开 navicat ,按照下图操做便可

新建表:
Entry表(Id为主键,勾选自增),Type_Id为外键.

EntryType表(Id为主键,勾选自增)
数据库

完事以后点击左上角的保存!api

在visual studio中创建控制台项目,安装必要的nuget包

打开nuget包管理工具,
在Browse选项卡中搜索 System.Data.SQLite
安装相应的nuget包,如图所示

以后在nuget包管理工具中查看已安装的nuget包
以下图:

而后在解决方案浏览器下能够看到App.config文件,

进去修改一下内容,在 provider 节点下加入下面的内容:
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
浏览器

创建 Entity 实体类

Entry 类 :

Entry
namespace MagicMemory.Core.Entities
{
    public class Entry
    {
        public int Id { get; set; }
        public string Key { get; set; }
        public string Content { get; set; }
        public int? EntryTypeId { get; set; }
        public virtual EntryType Type { get; set; }
    }
}
这里值得注意的是,Entry实体中有一个外键属性:

public virtual EntryType Type { get; set; }markdown

必定要用virtual来修饰,这里不清楚为啥,我也是偶然看见的,不用virual就没用,可能由于是Dbfirst的缘由,以前在.net core结合efcore使用的时候并不须要加virtual也行.这里的外键属性,Ef 会自动从数据库里相应的表中给咱们映射过来.可是这个外键所在表也必须在 DbContext中做为DbSet<>的泛型参数存在才能够.ide

EntryType 类:

EntryType
using System;

namespace MagicMemory.Core.Entities
{
    public class EntryType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

实体类就这两个,差很少能够说明问题了,创建实体类的时候,实体类的属性名必定要和表的字段名相匹配(必须同样才行),名称不同的话,则须要在属性的上方加一个注解属性 Column("Name").也可使用fluentApi来进行配置,我跟喜欢这种方式,在DbContext中重写OnModelCreating()方法,对 column 进行配置.函数

当实体类型的属性名须要和不一样名称的的表中的列进行映射时,可使用下面的方法.工具

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .Property(b => b.BlogId)
            .HasColumnName("blog_id");
    }

继承DbContext,创建数据库上下文 xxxContext 类

EntryContext
using System.Data.Entity;
using System.Data.SQLite;
using MagicMemory.Core.Entities;
using SQLite.CodeFirst;

namespace MagicMemory.DbStudy
{
    public class EntryContext:DbContext
    {
        public EntryContext():base(new SQLiteConnection("Data Source=MagicMemory.Db"),true)
        {
        }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            builder.Entity<Entry>().ToTable("Entry");
            builder.Entity<Entry>()
                .Property(e => e.EntryTypeId)
                .HasColumnName("EntryTypeId");

            builder.Entity<EntryType>().ToTable("EntryType");

            builder.Entity<EntryTag>().ToTable("EntryTagTable");
            builder.Entity<EntryTag>()
                .Property(t => t.Name)
                .HasColumnName("TagName");

            base.OnModelCreating(builder);

            Database.SetInitializer(new SqliteDropCreateDatabaseWhenModelChanges<EntryContext>(builder));
        }

        public DbSet<Entry> Entry { get; set; }
        public DbSet<EntryType> EntryType { get; set; }
        public DbSet<EntryTag> EntryTag { get; set; }
    }
}

这里比较重要的是重写了 OnModelCreating() 方法,这个方法里面经过 fluent api的方式定义了实体类的属性和具体的数据库的字段的映射的关系;同时,在默认的构造函数里面,调用基类的构造函数,由于使用sqlite这个数据库,因此将继承了DbConnection的实例: new SQLiteConnection("[链接字符串]") 传递给基类的构造函数,用来链接数据库.visual-studio

相关文章
相关标签/搜索