1

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 !

PeskyGnat
  • 2,454
  • 19
  • 22
Peter Smith
  • 5,528
  • 8
  • 51
  • 77
  • 2
    Makes no sense. EF is a Db-access framework. – H H May 27 '13 at 18:12
  • Thanks, I appreciate that. This is for future expansion. If I set it up this way I can then change it to a database in the future without change anything else. – Peter Smith May 27 '13 at 18:15
  • possible duplicate of [Entity Framework with text files (no database!)](http://stackoverflow.com/questions/6944357/entity-framework-with-text-files-no-database) – H H May 27 '13 at 18:18
  • 1
    The proper way to set this up for future expansion is to encapsulate the method of loading the data in a class. You would call a function with a signature similar to `IEnumerable LoadFromDataSource()` - inside this method you can then choose to use `EF` or `System.IO` operations to actually load the data. As long as your implementation of the `FlatFile` class remains the same you are free to change the details of how it's data is retrieved. – Nathan Anderson May 27 '13 at 18:19
  • Dangerous approach. The capability differences between the providers do leak upward. Consider the local-file file approach (MS-SQL). – H H May 27 '13 at 18:19
  • An embedded database should be easy enough to set up for a dev environment. – millimoose May 27 '13 at 18:27

1 Answers1

2

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.

Jay
  • 6,224
  • 4
  • 20
  • 23