I have the following method that will return VariantID for given VariantName (Name in db).To avoid calling my db few thousand times I have dumped all records (~30) to the simple dictionary.I'm almost certain that this is the correct way of implementing this method but I would like to hear some feedback and any possible tips.TestManagementEntities is an EntityFramework DbContextCalling Methodprivate void UpdateVariants(WorkItem workItem, string variantValue, TestManagementEntities db)    {                   string[] variants = variantValue.Split(',').Where(x => !String.IsNullOrEmpty(x)).ToArray();        int[] variantIds = new int[variants.Count()];        Dictionary<string, int> variantDictionary =            db.Variants.ToDictionary(x => x.Name, y => y.VariantID);        Object lockObject = new Object();        System.Threading.Tasks.Parallel.For(0, variants.Count(), i =>        {            variants[i] = variants[i].Trim();            //Method in question call            variantIds[i] = GetVariantId(variants[i], variantDictionary, db, lockObject);        });        //Some not relevant to question code here    }private int GetVariantId(string variantName, Dictionary<string, int> variantDictionary, TestManagementEntities db, Object lockObject)    {        if (variantDictionary.ContainsKey(variantName))        {            return variantDictionary[variantName];        }        else        {            lock (lockObject)            {                if (variantDictionary.ContainsKey(variantName))                {                    return variantDictionary[variantName];                }                else                {                    Variant newVariant = new Variant();                    newVariant.Name = variantName;                    db.Variants.Add(newVariant);                    db.SaveChanges();                    variantDictionary.Add(variantName, newVariant.VariantID);                    return newVariant.VariantID;                }            }        }    }
Thread-safe database updates
c#;entity framework;task parallel library