Loading

Visual Studio 2015使用EF6的DBFirst模式操作Sqlite数据库

  什么是DBFirst

 

  1:到官方下载并安装32位驱动(如果你是旧版的驱动,卸载掉,然后下载最新版的,否则操作数据时会出现异常)

  

  

  

  2:通过Nuget获取System.Data.SQLite(会默认把下面的这些依赖库都加上)

 

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.0.0" targetFramework="net40" />
  <package id="EntityFramework.zh-Hans" version="6.1.3" targetFramework="net40" />
  <package id="System.Data.SQLite" version="1.0.101.0" targetFramework="net40" />
  <package id="System.Data.SQLite.Core" version="1.0.101.0" targetFramework="net40" />
  <package id="System.Data.SQLite.EF6" version="1.0.101.0" targetFramework="net40" />
  <package id="System.Data.SQLite.Linq" version="1.0.101.0" targetFramework="net40" />
</packages>

  

  3:数据库存放于App_Data下

  

  4:添加实体模型

  

  

 

  5:修改配置文件路径(默认生成的字符串是用的磁盘绝对路径,最好换成下面这种直接访问App_Data目录的形式)

    

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<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>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </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" />
    </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>
    <!--注意这里的data source=|DataDirectory|-->
    <add name="JianBoShiEntities" connectionString="metadata=res://*/JianBoShiContent.csdl|res://*/JianBoShiContent.ssdl|res://*/JianBoShiContent.msl;provider=System.Data.SQLite.EF6;provider connection string="data source=|DataDirectory|\JianBoShi.db"" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

  

  

  6:调用

  

 using (var db = new JianBoShiEntities())
 {
     Repeater1.DataSource = db.ProductCategories.ToList();
     Repeater1.DataBind();
 }

  

  

posted @ 2016-06-14 10:16  shihao316558512  阅读(1779)  评论(0编辑  收藏  举报