private static void AddOrUpdateEnumeration <T>(ExperimentContext db, T taskState) where T : Enumeration { if (db.Set <T>().Any(s => s.Id == taskState.Id)) { db.Set <T>().Update(taskState); } else { db.Set <T>().Add(taskState); } }
private static void Main(string[] args) { using (ExperimentContext db = new ExperimentContext()) { //db.Database.EnsureDeleted(); //db.Database.EnsureCreated(); AddOrUpdateEnumeration(db, TaskState.Created); AddOrUpdateEnumeration(db, TaskState.Done); AddOrUpdateEnumeration(db, TaskState.InProgress); AddOrUpdateEnumeration(db, TaskPriority.Normal); AddOrUpdateEnumeration(db, TaskPriority.Low); AddOrUpdateEnumeration(db, TaskPriority.High); db.SaveChanges(SaveChangesMode.WithEnumeration); } using (ExperimentContext db = new ExperimentContext()) { db.Tasks.Add(new Model.TaskItem() { Title = "teszt4", State = TaskState.Done, Priority = TaskPriority.Normal }); db.SaveChanges(); } using (ExperimentContext db = new ExperimentContext()) { var taskItems = db.Tasks.Take(100).ToList(); FillEnumerations(taskItems); taskItems = db.Tasks.Take(100).ToList(); FillEnumerations(taskItems); } Console.WriteLine("Hello World!"); Console.ReadLine(); }