vs2015使用Sqlite数据库,并使用EntityFramework实体类进行映射

前言:因使用sqlite时发现,根据 EntityFramework创建ADO.NET实体类数据库时并未找到sqlite(本身是没有sqlite驱动的,只有sql server,oracle等数据库),所以才有了下面这些操作

步骤1、从网址https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki下载

sqlite-netFx46-setup-bundle-x86-2015-1.0.109.0.exe或者sqlite-netFx46-setup-bundle-x64-2015-1.0.109.0.exe

双击进行安装

 

 

 步骤2:从Nuget程序包管理器中下载相关包

 

步骤3:配置App.config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>

  <connectionStrings>
   <!--添加ADO.NET时自动生成(此步骤不需要配置这条),注意别忘了选择数据库文件-->
  <add name="数据库文件名称Entities" connectionString="metadata=res://*/db.MonitoringWarning.csdl|res://*/db.MonitoringWarning.ssdl|res://*/db.MonitoringWarning.msl;provider=System.Data.SQLite.EF6;provider connection string='data source=&quot;数据库文件所在路径\MonitoringWarning.db&quot;'" providerName="System.Data.EntityClient" /></connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
    <!--添加下面这句,不然可能报错-->
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
<system.data>
    <DbProviderFactories>
     <!--add标签是一定要在remove标签之后的-->
      <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></configuration>

步骤4:创建ADO.NET实体类数据库

 往下进行就行,相信用过的都会

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#中将实体类映射SQLite数据库,可以使用ORM(对象关系映射)框架,比如EF Core和Dapper等。以下是使用EF Core的一个简单示例: 1. 首先,需要安装EF Core和SQLite包。可以使用NuGet包管理器或手动下载并添加到项目中。 2. 创建SQLite数据库文件并打开它。可以使用SQLite Studio或其他SQLite客户端。 3. 创建一个实体类,例如: ```csharp public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } ``` 4. 创建DbContext类,指定连接字符串和实体类: ```csharp using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Data Source=database.db"); } } ``` 5. 在C#代码中,使用DbContext连接到SQLite数据库,并将实体类映射数据库表。以下是一个示例代码: ```csharp using System.Linq; //连接到SQLite数据库 using (var db = new MyDbContext()) { //创建Person表 db.Database.EnsureCreated(); //插入新记录 var person = new Person { Name = "John Doe", Age = 30 }; db.People.Add(person); db.SaveChanges(); //查询记录 var people = db.People.ToList(); //输出结果 foreach (var p in people) { Console.WriteLine($"Id: {p.Id}, Name: {p.Name}, Age: {p.Age}"); } } ``` 这样,就可以将实体类映射SQLite数据库中,可以方便地进行CRUD操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值