aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ObservablesContextConfiguration.cs
blob: 07c3f5306d343c0b17574bf919508fb77211df0f (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core.Helpers;

namespace Tango.BL
{
    public class ObservablesContextConfiguration : DbConfiguration
    {
        public static String FolderPath { get; private set; }

        static ObservablesContextConfiguration()
        {
            FolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "EFCache", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName));
        }

        public ObservablesContextConfiguration() : base()
        {
            Directory.CreateDirectory(FolderPath);
            var modelStore = new DefaultDbModelStore(FolderPath);

            try
            {
                modelStore.TryLoad(typeof(ObservablesContext));
            }
            catch
            {
                try
                {
                    Directory.Delete(FolderPath, true);
                    Thread.Sleep(1000);
                    Directory.CreateDirectory(FolderPath);
                }
                catch { }
                modelStore = new DefaultDbModelStore(FolderPath);
            }

            SetModelStore(modelStore);
        }

        public static void ClearModelStore()
        {
            try
            {
                Directory.Delete(FolderPath);
            }
            catch { }
        }
    }
}