static void Main(string[] args) { using (var db = new BloggingContext()) { //Create and save a new Blog Console.Write("Enter a new name for a new Blog: "); var name = Console.ReadLine(); var blog = new Blog { Name = name }; db.Blogs.Add(blog); db.SaveChanges(); //Display all blogs from the database var query = from b in db.Blogs orderby b.Name select b; Console.WriteLine("All blogs in the database: "); foreach (var item in query) { Console.WriteLine(item.Name); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
static void Main(string[] args) { using (var db = new BloggingContext()) { // Create and save a new Blog Console.Write("Enter Username: "******"Enter a name for a new Blog: "); var blogName = Console.ReadLine(); Console.Write("Enter a name for a new Organisation: "); var orgName = Console.ReadLine(); //Create entities var organization = new Organization { OrganizationName = orgName }; var user = new User { Username = userName, Organization = organization }; var blog = new Blog { Name = blogName }; db.Users.Add(user); db.Organizations.Add(organization); db.Blogs.Add(blog); db.SaveChanges(); // Query all Blogs from the database var blogQuery = from b in db.Blogs orderby b.Name select b; // Query all Users from the database var userQuery = from u in db.Users orderby u.Username select u; // Query all Users from the database var orgQuery = from o in db.Organizations orderby o.OrganizationName select o; Console.WriteLine("All blogs in the database:"); foreach (var item in userQuery) { Console.WriteLine(item.Username + " is a member of the organization: " + item.Organization.OrganizationName); } } }
static void Main(string[] args) { //Console.WriteLine("Input a blog name"); //var name = Console.ReadLine(); using (var context = new BloggingContext()) { //var blog = new Blog { Name = name }; //context.Blogs.Add(blog); //context.SaveChanges(); //var blogs = from b in context.Blogs // orderby b.Name // select b; //Console.WriteLine("All blogs in database:"); //foreach (var b in blogs) //{ // Console.WriteLine(b.Name); //} var tt = new TestTable { Content = "test content" }; context.Set <TestTable>().Add(tt); Console.WriteLine("1"); context.SaveChanges(); Console.WriteLine("Saved"); var ttable = context.Set <TestTable>().FirstOrDefault(); Console.WriteLine($"{ttable?.Content}"); Console.ReadKey(); } }
static void Main(string[] args) { using (var db = new BloggingContext()) { Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); var blog = new Blog { Name = name }; db.Blogs.Add(blog); db.SaveChanges(); var query = from b in db.Blogs orderby b.Name select b; Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Name); } Console.Write("Enter a name for a new Organization: "); var nameOrganization = Console.ReadLine(); var organization = new Organization { OrganizationName = nameOrganization }; db.Organizations.Add(organization); db.SaveChanges(); Console.Write("Enter a username for a new User: "******"All users and assoiated organizations in the database:"); foreach (var item in queryUser) { Console.WriteLine(item.Username + "\t" + item.OrganizationId); } var queryOrganization = from b in db.Organizations orderby b.OrganizationId select b; Console.WriteLine("All organizations in the database:"); foreach (var item in queryOrganization) { Console.WriteLine(item.OrganizationId + "\t" + item.OrganizationName); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
static void Main(string[] args) { using (var db = new BloggingContext()) { // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); var blog = new Blog { Name = name }; db.Blogs.Add(blog); Console.Write("Enter a name for a new Organization "); var organizationName = Console.ReadLine(); var organization = new Organization { OrganizationName = organizationName }; db.Organizations.Add(organization); Console.Write("Enter a users name to add to the organization "); var userName = Console.ReadLine(); var user = new User { Username = userName, Organization = organization }; db.Users.Add(user); db.SaveChanges(); // Display all Blogs from the database var query = from b in db.Blogs orderby b.Name select b; var query2 = from b in db.Users orderby b.Username select b; Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Name); } Console.WriteLine("Users and organizations "); foreach (var item in query2) { Console.WriteLine(item.Username + " works in " + item.Organization.OrganizationName); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
static void Main(string[] args) { using (var db = new BloggingContext()) { //Kontrol af Punkt 21 om det virkede var o_query = from o in db.Organizations orderby o.OrganizationId select o; foreach (var o_item in o_query) { Console.WriteLine("Org: " + o_item.OrganizationName + " Homeland 1: " + o_item.Homelands[0].CountryName + " Homeland 2: " + o_item.Homelands[1].CountryName + " Homeland 3: " + o_item.Homelands[2].CountryName); } // Create og save en ny Blog Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); //Add et nyt navn til bloggen DB var blog = new Blog { Name = name }; db.Blogs.Add(blog); db.SaveChanges(); //Udvidelse af organizationsnavn Console.WriteLine("Please enter a new organization name."); var orgname = Console.ReadLine(); //Add nyt navn til organizationstabellen var org = new Organization { OrganizationName = orgname }; db.Organizations.Add(org); db.SaveChanges(); //Færdig med første udvidelse //Anden udvidelse, tilsæt brugernavn til organisationen (tror jeg) Console.WriteLine("Enter your username, it will be added to the org"); var username = Console.ReadLine(); //Add nyt navn til brugertabellen var user = new User { Username = username, Organizations = org }; //Tilføjer med reference organizations = org db.Users.Add(user); db.SaveChanges(); // Display alle Blogs fra database var query = from b in db.Blogs orderby b.Name select b; Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Name); } //Punkt 8, udskriv alle bruger og organizationer var u_query = from u in db.Users orderby u.Username select u; Console.WriteLine("Usernames, organizations and orgID"); foreach (var u_item in u_query) { Console.WriteLine("User: "******" Org: " + u_item.Organizations.OrganizationName + " Org ID: " + u_item.Organizations.OrganizationId); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }