static int AddTask(AsanaContext db) { var task = db.Tasks.Where(t => t.Name == "Make the tea").FirstOrDefault(); if (task == null) { task = db.Tasks.Create(); task.Name = "Make the tea"; task.CreatedAt = DateTime.Now; task.Followers.Add(db.Followers.Add(new Follower { User = db.Users.Where(u => u.Name == "Frank Zappa").FirstOrDefault() })); task.Followers.Add(db.Followers.Add(new Follower { User = db.Users.Where(u => u.Name == "Tom Waits").FirstOrDefault() })); db.Tasks.Add(task); } return db.SaveChanges(); }
static int AddUsers(AsanaContext db) { if (db.Users.Where(u => u.Name == "Frank Zappa").FirstOrDefault() == null) { db.Users.Add(new AsanaUser { Name = "Frank Zappa", Email = "*****@*****.**" }); } if (db.Users.Where(u => u.Name == "Bob Dylan").FirstOrDefault() == null) { db.Users.Add(new AsanaUser { Name = "Bob Dylan", Email = "*****@*****.**" }); } if (db.Users.Where(u => u.Name == "Tom Waits").FirstOrDefault() == null) { db.Users.Add(new AsanaUser { Name = "Tom Waits", Email = "*****@*****.**" }); } return db.SaveChanges(); }
static void Main(string[] args) { // Database.SetInitializer<AsanaContext>(new DropCreateDatabaseAlways<AsanaContext>()); //Database.SetInitializer<AsanaContext>(new DropCreateDatabaseIfModelChanges<AsanaContext>()); using (var db = new AsanaContext()) { // create users Console.WriteLine(AddUsers(db)); Console.WriteLine(AddTask(db)); var task = db.Tasks.Where(t => t.Name == "Make the tea").FirstOrDefault(); foreach (var f in task.Followers) { Console.WriteLine(f.User.Name); } Console.ReadLine(); } }