微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。