Ejemplo n.º 1
0
        public static Task SeedData(this WebsitesContext context)
        {
            return(Task.Run(() =>
            {
                if (context.Database.IsSqlServer())
                {
                    context.Database.EnsureCreated();                                 // For MSSQL only
                }
                if (context.Websites.Any())
                {
                    //To truncate tables and reseed the identities -- only required few times and not always
                    context.Websites.RemoveRange(context.Websites);
                    context.SaveChanges();
                    if (context.Database.IsSqlServer()) // For MSSQL only
                    {
                        context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('Websites', RESEED, 0)");
                        context.Database.ExecuteSqlCommand("DBCC CHECKIDENT('WebsiteDetails', RESEED, 0)");
                    }
                }

                if (!context.Websites.Any())
                {
                    context.Websites.AddRange(getWebsites().GetAwaiter().GetResult());
                    context.SaveChanges();
                }
                if (!context.WebsiteDetails.Any())
                {
                    context.WebsiteDetails.AddRange(getWebsiteDetails(context).GetAwaiter().GetResult());
                    context.SaveChanges();
                }
            }));
        }
Ejemplo n.º 2
0
 public static Task <List <WebsiteDetail> > getWebsiteDetails(WebsitesContext context) =>
 Task.Run(() => context.Websites
          .ToList <Website>()
          .Select((website, index) =>
                  Enumerable.Range(-15, 30)
                  .Select((number, enuIndex) => new WebsiteDetail {
     WebsiteId = website.WebsiteId, VisitDate = DateTime.Now.AddDays(number), TotalVisits = (index + 1) * (enuIndex + 1) * 100, Website = website
 })
                  .ToList <WebsiteDetail>()
                  )
          .SelectMany(websiteDetails => websiteDetails)
          .ToList <WebsiteDetail>()
          );