public PlantVariety CreatePlantVariety(PlantVariety plantVariety) { using (var database = new Core.Entity.SeedsEntities()) { var entity = plantVariety.ToEntity(database); entity.ID = database.PlantVariety.Execute(MergeOption.NoTracking).Any() ? database.PlantVariety.Execute(MergeOption.NoTracking).Max(pv => pv.ID) + 1 : 0; database.AddToPlantVariety(entity); database.SaveChanges(); var newEntity = database.PlantVariety.Execute(MergeOption.NoTracking).Single(v => v.ID == entity.ID); var result = new PlantVariety(newEntity); var plant = Cache.Instance.Plants[newEntity.PlantID]; result.Plant = plant; plant.Varieties[result.ID] = result; return result; } }