Entity Framework 6/EF Core 连接文件类型数据源(Json/xml/MongoDB)
2021/5/11 19:27:41
本文主要是介绍Entity Framework 6/EF Core 连接文件类型数据源(Json/xml/MongoDB),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
最近工作上有个要求,要用Entity Framework 对Json类型文件操作,来自动更改配置项,但是之前都是用EF来映射的数据库,还没有对文件做过映射,又重新去读了MSDN的文档才知道,原来也是有相关的资料的;
下面是用法,文档链接在最后;
Entity Framework 6
C#引用Nuget包 filecontext
文件类型
PM> install-package filecontext
mongodb数据库
PM> install-package filecontext.mongodb
替换原有的DBContext改为FileContext
//public class ContentItemContext : DbContext public class ContentItemContext : FileContext { public ContentItemContext() : base(new JsonStoreStrategy()) // or DefaultStoreStrategy() => XmlStoreStrategy() // or InMemoryStoreStrategy() { } public FileSet<ContentItem> ContentItems { get; set; } //public DbSet<ContentItem> ContentItems { get; set; } }
接下来就能像是用EF操作数据库一样操作文档了;
Entity Framework Core
EFCore和EF6一样,只不过是Nuget包命令名称有所不同
PM > Install-Package FileContextCore
Nuget包源码地址:
EF Core:morrisjdev/FileContextCore: FileContextCore is a "Database"-Provider for Entity Framework Core and adds the ability to store information in files instead of being limited to databases. (github.com)
EF 6:pmizel/DevMentor.Context.FileContext: FileContext is a data access layer (DAL) Framework for rapid data driven application development (RDDAD). (github.com)
这个包是开源的,如果有需要可以自己去拉下来包自己改动下
参考链接:
数据库提供程序 - EF Core | Microsoft Docs
这篇关于Entity Framework 6/EF Core 连接文件类型数据源(Json/xml/MongoDB)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set