Microsoft Entity Framework使用平面文件作为数据源 [英] Microsoft Entity Framework using a flat file as a data source

查看:136
本文介绍了Microsoft Entity Framework使用平面文件作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2010与c#4.01,MVC3和实体框架5。



我想使用只读文本文件作为EF数据源。我的简单数据结构是:

  public class FlatFile 
{
public string Caption {get;组; }
public string Filename {get;组; }
}

我似乎有一个关于如何用EF设置的块。帮帮我!谢谢!

解决方案

总结上述评论中的大部分内容: EF 旨在查询数据库并返回所需数据的子集。简单地序列化和反序列化整个数据集就是你想将数据备份到一个平面文件中的真正需要的。如果您的数据集变得太大,这可能是一个危险的路径。还有一点必须解决的事情,以确保你的网络环境线程安全。



然而,似乎你需要一个垫脚石为您的开发,开始简单地和最终升级到真实数据库,而不必重写一堆代码。由于您提到MVC3,我强烈建议您查看 IOC / DI 工具,如 Ninject 。这样,您可以定义所需的接口,并在准备就绪时简单地交换实现。我还建议尝试 代码优先 作为相对于平面文件,默认情况下它将使用本地数据存储,稍后可能会向数据库提供更平滑的转换。


I am using Visual Studio 2010 with c# 4.01, MVC3 and Entity Framework 5.

I want to use a read-only text file as an EF data-source. My simple data structure is:

public class FlatFile
{
    public string Caption {get; set; }
    public string Filename {get; set; }
}

I seem to have a block as to how to set this up with EF. Help! and thanks !

解决方案

To sum up most of whats in the comments above: EF is designed to query against databases and return subsets of the data that is needed. Simply serializing and de-serializing your entire dataset is all you will really need if you want to back your data into a flat file. This can be a dangerous path to take if your dataset becomes too large. There are also little tidbits that must be addressed to make sure you are thread safe for your web environment.

However, it seems like you need a stepping stone for your development, starting simply and eventual upgrading to a 'real' database, without having to rewrite a bunch of code. Since you mentioned MVC3, I would highly recommend checking out an IOC/DI tool like Ninject. This way you can define the interfaces needed and simply swap out the implementation when you are ready. I would also recommend trying Code First as opposed to a flat file, by default it will use a local data store and may provide a smoother transition to a database later.

这篇关于Microsoft Entity Framework使用平面文件作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆