如何用Entity Framework 6 连接sqlite3连接数据库数据库

如何用Entity Framework 6 连接Sqlite数据库_百度知道
如何用Entity Framework 6 连接Sqlite数据库
提问者采纳
一个安装的DLL里面包含SQLite.Interop。  配置config文件  如果你是用NuGet获取Sqlite。就像我的程序中实体名是Person.Core.dll”的错误,EF生成的SQL表都是用复数表示?  注意:  注意这里面每个,一个没有。如果你运行代码的时候报  “无法加载SQLite.dll拷贝到Bin文件中即可。所以在数据库中定义的表名是People.Interop.dll,它也会自动下载EF6  2。或是在NuGet下载的packages&#92.可以用NuGet程序包来获取.Data.db来配置在App_Data文件平中,而另一个没有,则将安装文件中的SQLite,但EF去查找的表名会是People.94.0,您需要自己先设计好Sqlite的表结构.0&#92.在Sqlite官网上下载对应的版本。  2.db要在Bin文件夹中;System。;SqliteTbuild中也有对应的程序.连接串的配置。  数据连接串可以指定绝对地址.SQLite,如果是web程序可以通过Data Source=|DataDirectory|&#92。像我的data source=SqliteTest.db,则SqliteTest.1,也可以指定相对地址.不支持CodeFirst模式,会自动在config中配置一些相关的信息.net framework都有两个版本.Interop,一个带有bundle字眼 获取Sqlite  1。  3.如果没有指定数据库中的表文件名:  1
资深电脑人
其他类似问题
为您推荐:
sqlite的相关知识
其他1条回答
有相关API的。你是用modelfirst嘛?
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁4507人阅读
编程经验记录(11)
C#与.NET(65)
数据库(37)
&!--这里必须注册System.Data.SQLite,否则找不到,除非加入GAC,DbConfiguration配置也需要--&
& &system.data&
& & &DbProviderFactories&
& & & &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&/&
& & & &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&/&
& & &/DbProviderFactories&
& &/system.data&
&connectionStrings&
& & &add name=&northwindEFEntities& connectionString=&data source=.\northwindEF.db& providerName=&System.Data.SQLite&/&
& &/connectionStrings&
&entityFramework&
& & &providers&
& & & &provider invariantName=&System.Data.SQLite& type=&System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6&/&
& & &/providers&
& &/entityFramework&
因为用到了EF,所以这里的entityFramework配置里面的invariantName与connectionStrings里面的providerName都要对应为DbProviderFactories里面注册的名字invariant=&System.Data.SQLite&,否则会提示没有注册System.Data.SQLite。
并且&&DbProviderFactories&节必须有,否则当目标计算机没有GAC注册System.Data.SQLite,会提示找不到。。。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:108866次
积分:2216
积分:2216
排名:第11219名
原创:112篇
转载:28篇
评论:12条
由于作者水平有限,如果在您阅读博客过程中发现问题,请留言!作者非常感谢您的帮助!
(1)(1)(3)(1)(3)(2)(1)(3)(1)(1)(4)(1)(6)(3)(2)(11)(3)(3)(2)(6)(5)(5)(3)(3)(6)(12)(3)(2)(1)(8)(6)(6)(3)(1)(1)(6)(1)(3)(4)(1)(6)&&&&Entity Framework 6以模型方式搭建Sqlite数据库开发环境
Entity Framework 6以模型方式搭建Sqlite数据库开发环境
Entity Framework 6以模型方式搭建Sqlite数据库环境
Visual studio 2010 .net40
未处理 System.Configuration.ConfigurationErrorsException
Message=找不到或无法加载已注册的 .Net Framework Data Provider。
Source=System.Data
BareMessage=找不到或无法加载已注册的 .Net Framework Data Provider。
未处理 System.InvalidCastException
Message=无法将类型为“System.Data.SQLite.SQLiteConnection”的对象强制转换为类型“System.Data.Entity.Core.EntityClient.EntityConnection”。
Source=EntityFramework
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
数据库下载排行Entity Framework 6 Code First 系列:SQLite.CodeFirst自动生成数据库 - 推酷
Entity Framework 6 Code First 系列:SQLite.CodeFirst自动生成数据库
在Code First模式下使用SQLite一直存在 不能自动生成数据库的问题 ,使用SQL Server Compact再转换到SQLite的方式(SQL Server Compact/SQLite Toolbox插件)基本不在我的考虑范围内,直接使用SQL Server Compact性能又是问题。理论上我们可以自己去实现SQLite的Code Frist支持,但实际上我只是在等待它的出现。期待了一年多,
真的出现了。
1.首先定义实体:Customer、Role、Category、Post。
public class BaseEntity
public int Id { get; set; }
public class Customer : BaseEntity
public Customer()
this.Roles = new List&Role&();
public string UserName { get; set; }
public virtual ICollection&Role& Roles { get; set; }
public class Role : BaseEntity
public Role()
this.Customers = new List&Customer&();
public virtual ICollection&Customer& Customers { get; set; }
public string RoleName { get; set; }
public class Category : BaseEntity
public Category()
this.Children = new List&Category&();
this.Posts = new List&Post&();
public int? ParentId { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection&Category& Children { get; set; }
public virtual ICollection&Post& Posts { get; set; }
public class Post : BaseEntity
public virtual Category Category { get; set; }
2.定义实体映射:CustomerMap、RoleMap、CategoryMap和PostMap作为关系表、索引的配置。
public class CustomerMap : EntityTypeConfiguration&Customer&
public CustomerMap()
this.Property(o =& o.UserName).HasColumnAnnotation(&Index&, new IndexAnnotation(new IndexAttribute() { IsUnique = true }));
public class RolerMap : EntityTypeConfiguration&Role&
public RolerMap()
this.HasMany(o =& o.Customers).WithMany(o =& o.Roles);
public class CategoryMap : EntityTypeConfiguration&Category&
public CategoryMap()
this.HasOptional(o =& o.Parent).WithMany(o =& o.Children).HasForeignKey(o =& o.ParentId);
public class PostMap : EntityTypeConfiguration&Post&
public PostMap()
this.HasOptional(o =& o.Category).WithMany(o =& o.Posts);
3.定义初始化数据:目前SQLite.CodeFist只支持DropCreateDatabaseAlways和CreateDatabaseIfNotExists方式,从issues上看也就是几天的事。
public class MyDbInitializer : SqliteDropCreateDatabaseAlways&SqliteDbContext&
public MyDbInitializer(string connectionString, DbModelBuilder modelBuilder)
: base(connectionString, modelBuilder) { }
protected override void Seed(SqliteDbContext context)
context.Set&Customer&().Add(new Customer { UserName = &user& + DateTime.Now.Ticks.ToString(), Roles = new List&Role& { new Role { RoleName = &user& } } });
context.Set&Post&().Add(new Post { Category = new Category() });
base.Seed(context);
4.定义DbContext:此处必须配置 PluralizingTableNameConvention ,否则无法正常使用。
public class SqliteDbContext : DbContext
public SqliteDbContext()
: base(&DefaultConnection&)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Conventions.Remove&PluralizingTableNameConvention&();
modelBuilder.Configurations.AddFromAssembly(typeof(SqliteDbContext).Assembly);
Database.SetInitializer(new MyDbInitializer(Database.Connection.ConnectionString, modelBuilder));
5.配置Web.config:默认的配置文件各种问题。可以直接拷贝项目中的测试用的配置文件。
&?xml version=&1.0& encoding=&utf-8&?&
有关如何配置 ASP.NET 应用程序的详细信息,请访问
/fwlink/?LinkId=301880
&configuration&
&configSections&
&!-- For more information on Entity Framework configuration, visit /fwlink/?LinkID=237468 --&
&section name=&entityFramework& type=&System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c& requirePermission=&false& /&
&/configSections&
&appSettings&
&add key=&webpages:Version& value=&3.0.0.0& /&
&add key=&webpages:Enabled& value=&false& /&
&add key=&ClientValidationEnabled& value=&true& /&
&add key=&UnobtrusiveJavaScriptEnabled& value=&true& /&
&/appSettings&
&connectionStrings&
&add name=&DefaultConnection& connectionString=&data source=|DataDirectory|\db.sqlite& providerName=&System.Data.SQLite& /&
&/connectionStrings&
&system.web&
&compilation debug=&true& targetFramework=&4.5& /&
&httpRuntime targetFramework=&4.5& /&
&/system.web&
&assemblyBinding xmlns=&urn:schemas-microsoft-com:asm.v1&&
&dependentAssembly&
&assemblyIdentity name=&Microsoft.Owin& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-3.0.0.0& newVersion=&3.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&Microsoft.Owin.Security.OAuth& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-3.0.0.0& newVersion=&3.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&Microsoft.Owin.Security.Cookies& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-3.0.0.0& newVersion=&3.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&Microsoft.Owin.Security& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-3.0.0.0& newVersion=&3.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&Newtonsoft.Json& culture=&neutral& publicKeyToken=&30ad4fe6b2a6aeed& /&
&bindingRedirect oldVersion=&0.0.0.0-6.0.0.0& newVersion=&6.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&System.Web.Optimization& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-1.1.0.0& newVersion=&1.1.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&WebGrease& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-1.5.2.14234& newVersion=&1.5.2.14234& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&System.Web.Helpers& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-3.0.0.0& newVersion=&3.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&System.Web.WebPages& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-3.0.0.0& newVersion=&3.0.0.0& /&
&/dependentAssembly&
&dependentAssembly&
&assemblyIdentity name=&System.Web.Mvc& publicKeyToken=&31bf& /&
&bindingRedirect oldVersion=&1.0.0.0-5.2.2.0& newVersion=&5.2.2.0& /&
&/dependentAssembly&
&/assemblyBinding&
&/runtime&
&system.data&
&DbProviderFactories&
&remove invariant=&System.Data.SQLite&/&
&remove invariant=&System.Data.SQLite.EF6& /&
&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&
&entityFramework&
&defaultConnectionFactory type=&System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework&&
&parameters&
&parameter value=&mssqllocaldb& /&
&/parameters&
&/defaultConnectionFactory&
&providers&
&provider invariantName=&System.Data.SqlClient& type=&System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer& /&
&provider invariantName=&System.Data.SQLite& type=&System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6& /&
&/providers&
&/entityFramework&
&/configuration&
6.配置Global.asax。
public class MvcApplication : System.Web.HttpApplication
protected void Application_Start()
using (var db = new SqliteDbContext())
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
查看生成的数据库:表映射、关系映射和索引都正确创建了。
调用一下:
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
没有分页内容
图片无法显示
视频无法显示
与原文不一致如何用Entity Framework 6 连接Sqlite_百度知道
如何用Entity Framework 6 连接Sqlite
获取Sqlite  1.可以用NuGet程序包来获取,它也会自动下载EF6  2.在Sqlite官网上下载对应的版本:  注意这里面每个.net framework都有两个版本,一个带有bundle字眼,一个没有。一个安装的DLL里面包含SQLite.Interop.dll,而另一个没有。如果你运行代码的时候报  “无法加载SQLite.Interop.dll”的错误,则将安装文件中的SQLite.Interop.dll拷贝到Bin文件中即可。或是在NuGet下载的packages\System.Data.SQLite.Core.1.0.94.0\build中也有对应的程序。      注意:  1.连接串的配置。  数据连接串可以指定绝对地址,也可以指定相对地址。像我的data source=SqliteTest.db,则SqliteTest.db要在Bin文件夹中,如果是web程序可以通过Data Source=|DataDirectory|\SqliteTest.db来配置在App_Data文件平中。  2.如果没有指定数据库中的表文件名,EF生成的SQL表都是用复数表示。就像我的程序中实体名是Person,但EF去查找的表名会是People。所以在数据库中定义的表名是People。  3.不支持CodeFirst模式,您需要自己先设计好Sqlite的表结构。
其他类似问题
为您推荐:
sqlite的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 php连接sqlite数据库 的文章

 

随机推荐