3

Given a People table like this:

+----+------+
| ID | Name |
+----+------+
|  1 | Foo  |
|  2 | Bar  |
|  3 | Baz  |
+----+------+

I'd like to generate c# code like this:

List<People> foo = new List<People> 
{
    new People{ID = 1, Name = "Foo" },
    new People{ID = 2, Name = "Bar" },
    new People{ID = 3, Name = "Baz" }
};

The intent is to use the database to easily create large sets of data for unit testing with mock objects.

For clarification, I am using Entity Framework, and I'm using EntityFramework.Testing with Moq to generate mock objects for testing.

Please note that I'm not trying to get an instance of a collection of data from the database, but rather trying to generate an initializer (like above) that can be used as a basis for unit testing without being connected to the database. In other words, the output of whatever tool/process/code you propose should be c# code that represents an object or collection initializer.

LockeCJ
  • 558
  • 1
  • 4
  • 21

2 Answers2

1

It looks like Object Exporter (from this answer) goes a long way towards this. It would require writing throwaway code to pull data in order to "export" it, but it's not too far away from the solution I'm looking for.

Community
  • 1
  • 1
LockeCJ
  • 558
  • 1
  • 4
  • 21
  • 1
    Hey @LockeCJ, glad Object Exporter could help you out, I created it mainly for Unit Testing purposes (extracting an object from a db). I'm working on adding better support for EF to ignore some of the dynamic proxies from EF, this should make it a lot easier to just export an EF object. – Omar Elabd Oct 23 '15 at 17:06
0

I'm using entityframework for describing for you:

var ListOfObjects = _YourContext.People.Where(q => q.id > 0).ToList();

in ListOfObjects are all of People which you inserted in People table like foo list which you mentioned in your question.

hope you have worked with entity framework.

vahid kargar
  • 800
  • 1
  • 9
  • 23