public static void Initialize(DisplayMonkeyEntities _db) { // refresh settings context ((System.Data.Entity.Infrastructure.IObjectContextAdapter)_db) .ObjectContext .Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, _db.Settings) ; var inDb = _db.Settings .Select(s => s.Key) .ToList() ; foreach (var k in _keyRes) { if (!inDb.Exists(s => s == k.Key)) { Setting setting = new Setting() { Key = k.Key, AnyValue = (k.Value as SettingProperties).DefaultValue, }; _db.Settings.Add(setting); _db.SaveChanges(); } } }
public static void SaveSetting(DisplayMonkeyEntities _db, Setting.Keys _id, object _value) { Guid key = _keyGuids[(int)_id]; Setting setting = _db.Settings.FirstOrDefault(s => s.Key == key); if (setting == null) { setting = new Setting() { Key = key, AnyValue = _value, }; _db.Settings.Add(setting); } else { setting.AnyValue = _value; _db.Entry(setting).State = System.Data.Entity.EntityState.Modified; } _db.SaveChanges(); }