Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export data as C# / VB format to use them in EF Code First's Database Initialization

Some times, sample data is important but boring to generate due to many related tables. So, in Entity Framework Code First, I think it's an elegant way to insert them in DropCreateDatabaseIfModelChanges::Seed().

But is there a way to export existing data from database back to a string as C# / VB sentences that insert poco objects?

If it works, we may use it to backup data gracefully, or save to multiple .cs by scenes, switch them as needed to better our test, etc.

DATABASE EXPORTS AS THE FOLLOWING CODE:

var students = new List<Student>
{
    new Student { FirstMidName = "Carson",   LastName = "Alexander", },
    new Student { FirstMidName = "Meredith", LastName = "Alonso",    },
    new Student { FirstMidName = "Arturo",   LastName = "Anand",     },
    // ...
};
students.ForEach(s => context.Students.Add(s));
context.SaveChanges();
like image 676
victorwoo Avatar asked Aug 07 '12 02:08

victorwoo


People also ask

How do I export packet data to a C program?

The "Export as C Arrays (packet bytes) file" dialog box Export packet bytes into C arrays so you can import the stream data into your own C program. Export to file: frame chooses the file to export the packet data to. The Packet Range frame is described in Section 5.9, “The Packet Range frame” . 5.7.5. The "Export as PSML File" dialog box

How do I export data to CSV?

1. Reports The simplest option is to create a report then export the details as .csv. When viewing a report: Choose Comma Delimited .csv as the export file format 2. Data Export The next simplest way to mass export data to .csv is to use the Data Export scheduling tool.

How to export data of specific rows and columns from one excel?

With Aspose.Cells library for .NET, you can easily export data of specific rows and columns from one Excel document to another. The following code sample shows how to do this in C# language. // Open the source excel file. Workbook srcWorkbook = new Workbook ("Source_Workbook.xlsx"); // Create the destination excel file.

How to export DataTable to CSV using ADO?

If you're new to ADO.NET and DataTable, first read this: DataTable in C# Code: b. Create UI to display DataTable Here we created a simple DataGridView to bind to the DataTable. Code After adding the Extension method the ToCSV method is now appearing in the list below: d. Export to CSV on button click e. Call ToCSV method


1 Answers

As @Luke suggested in his comment. the best way is to generate Json files for your seed data.

in this article, you can find the easy way to generate your Json from SQL query.

Then all you need is to read them by Newtonsoft.Json

e.g.

var countriesStream = new StreamReader(HostingEnvironment.MapPath("jsonFile.json"));
try
{
    countriesList = JsonConvert.DeserializeObject<List<Country>>(countriesStream.ReadToEnd());
}
like image 156
Wahid Bitar Avatar answered Sep 28 '22 00:09

Wahid Bitar