Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created March 11, 2017 14:46
Show Gist options
  • Save Tangrila-BG/066d9bbd424525a52a6bbf1e15d3ee9a to your computer and use it in GitHub Desktop.
Save Tangrila-BG/066d9bbd424525a52a6bbf1e15d3ee9a to your computer and use it in GitHub Desktop.
Initial seed
using System;
using System.Linq;
namespace StudentDatabase
{
using System.Data.Entity;
using Models;
class StudentDbCreateIfNotExists : CreateDatabaseIfNotExists<StudentContext>
{
protected override void Seed(StudentContext context)
{
var rnd = new Random();
string[] studentNames =
{
"Pesho", "Gosho", "David", "Goliath",
"Martina", "Martin", "Vesko", "Kosio"
};
string[] studentPhones =
{
"0841234523", "0877963412", "2096883123",
"0031239412", "3214324552", "2321343124", null
};
string[] coursesNames =
{
"Programirane", "Surbane s vilica", "Topologiq",
"Matematika", "Kak da znaem kakvo ne znaem",
"Mikrotehnika"
};
string[] coursesDescriptions =
{
"Elate da vi vzemem parata", "Vsichki shte ostanem dovolni",
"Nqma takova neshto"
};
string[] resourcesNames =
{
"Chushki", "domati", "kasetki", "bira", "salata"
};
string[] resourcesUrls =
{
"http://zdrasti.com", "www.HelloWorld.random", "neznam.bg",
"http://abv.bg", "http://microsoft.com"
};
for (int i = 0; i < 5; ++i)
{
context.Students.Add(new Student()
{
Name = studentNames[rnd.Next(0, studentNames.Length)],
PhoneNumber = studentPhones[rnd.Next(0, studentPhones.Length)],
Birthday = new DateTime(rnd.Next(1980, 1990), rnd.Next(1, 12), rnd.Next(1, 30)),
RegistrationDate = new DateTime(rnd.Next(2002, 2008), rnd.Next(1, 12), rnd.Next(1, 30))
});
var courseStartDate = new DateTime(rnd.Next(2009, 2016), rnd.Next(1, 12), rnd.Next(1, 30));
context.Courses.Add(new Course()
{
Name = coursesNames[rnd.Next(0, coursesNames.Length)],
Description = coursesDescriptions[rnd.Next(0, coursesDescriptions.Length)],
StartDate = courseStartDate,
EndDate = courseStartDate.AddDays(rnd.Next(20, 60)),
Price = rnd.Next(1000, 3000) / 5.4m
});
}
for (int i = 0; i < 20; i++)
{
context.Resources.Add(new Resource()
{
Name = resourcesNames[rnd.Next(0, resourcesNames.Length)],
Url = resourcesUrls[rnd.Next(0, resourcesUrls.Length)],
ResourceType = (ResourceType) Enum.GetValues(typeof(ResourceType))
.GetValue(rnd.Next(0, Enum.GetValues(typeof(ResourceType)).Length)),
CourseId = rnd.Next(1, 6)
});
context.Homeworks.Add(new Homework()
{
Content = new String('1', 10002)
.ToCharArray()
.Select(x => byte.Parse(x.ToString()))
.ToArray(),
ContentType = (ContentType) Enum.GetValues(typeof(ContentType))
.GetValue(rnd.Next(0, Enum.GetValues(typeof(ContentType)).Length)),
SubmissionDate = new DateTime(rnd.Next(2010, 2016), rnd.Next(1, 12), rnd.Next(1, 30)),
CourseId = rnd.Next(1, 6),
StudentId = rnd.Next(1, 6)
});
context.Students.Find(rnd.Next(1, 6))
.Courses
.Add(context.Courses.Find(rnd.Next(1, 6)));
}
base.Seed(context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment