zoukankan      html  css  js  c++  java
  • 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();
     }
    

      

      

    欢迎大家多多交流
  • 相关阅读:
    Freedur为什么会免费?
    Cocos2d-x中使用音频CocosDenshion引擎介绍与音频文件的预处理
    AssetManager asset的使用
    Android AsyncHttpClient
    UI标签库专题十三:JEECG智能开发平台 ckfinder(ckfinder插件标签)
    UBUNTU 下如何升级 gcc, g++
    java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类、内部类应用于泛型探讨
    最新Connectify注冊码(序列号) Connectify3.7序列号 破解版
    简单单元測试思想
    Dijkstra算法
  • 原文地址:https://www.cnblogs.com/shihao316558512/p/5583077.html
Copyright © 2011-2022 走看看