-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSaveToDb.cs
44 lines (39 loc) · 1.54 KB
/
SaveToDb.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using EdiFabric.Core.Model.Edi;
using EdiFabric.Examples.EDIFACT.Common;
using EdiFabric.Framework.Readers;
using EdiFabric.Templates.EdifactD96A;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
namespace EdiFabric.Examples.EDIFACT.DB
{
class SaveToDb
{
/// <summary>
/// Save to DB with code first - this will create a new database the first time
/// Edit the connection string in app.config, by default it looks for a local SQL Server instance
/// </summary>
public static void Run()
{
Debug.WriteLine("******************************");
Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
Debug.WriteLine("******************************");
Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + Config.TestFilesPath + @"\EDIFACT\PurchaseOrder.txt");
List<IEdiItem> ediItems;
using (var ediReader = new EdifactReader(ediStream, "EdiFabric.Templates.Edifact"))
ediItems = ediReader.ReadToEnd().ToList();
var purchaseOrders = ediItems.OfType<TSORDERS>();
using (var db = new EDIFACTContext())
{
foreach (var purchaseOrder in purchaseOrders)
{
purchaseOrder.ClearCache();
db.TSORDERS.Add(purchaseOrder);
}
db.SaveChanges();
}
}
}
}