public void CreateMetric() { var appFactory = new ApplicationFactory(); using (var scoped = appFactory.CreateScope()) using (var db = scoped.ServiceProvider.GetRequiredService <Model.ModelDataContext>()) { var author = db.Users.FirstOrDefault(u => u.UserName == "dqm1"); var metric = new Metric { Author = author, AuthorID = author.ID, Justification = "I need metrics to test with.", Title = "The Best ever metric in the world first", Description = "This is a wickedly awesome description of a silly little metric that will cause everyone to have headaches.", ExpectedResults = "This should have some type of results." }; metric.ResultsTypeID = db.MetricResultTypes.Select(rt => rt.ID).First(); metric.AddDomains(db.Domains.Where(d => d.Title == "Demographics" || d.Title == "Enrollment").ToArray()); metric.AddFrameworkCategories(db.DataQualityFrameworkCategories.Where(f => f.Title == "Conformance").ToArray()); metric.Statuses.Add(new MetricStatusItem { Metric = metric, MetricStatus = db.MetricStatuses.Where(s => s.Title == "Draft").First(), User = author }); db.Metrics.Add(metric); db.SaveChanges(); } }