Пример #1
0
        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);

                }

            }
        }
Пример #2
0
        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();

            }
        }