Beispiel #1
0
        public Post Update(Post entity)
        {
            UnitOfWork.Posts.Update(entity);
            UnitOfWork.SaveChanges();

            // return updated entity from database
            return UnitOfWork.Posts.Get(entity.Id);
        }
Beispiel #2
0
        public Post Create(Post entity)
        {
            UnitOfWork.Posts.Add(entity);
            UnitOfWork.SaveChanges();

            // return created entity from database
            return UnitOfWork.Posts.Get(entity.Id);
        }
Beispiel #3
0
        // Instruction:
        // 1. Create database in, for example, Management studio
        // 2. Install Entity Framework NuGet Package to VS-project
        // 3. Add to project -> New Item -> Data -> ADO.NET Entity Data Model
        // 4. Transfer Database to project and use by ORM-technology
        // 5. Edit database by code and create migrations + database updates

        // Migrations commands: 
        // Enable-Migration (first calling only)
        // Add-Migration {Migration Name}
        // Update-Database


        public static void Main()
        {
            using (var db = new BlogContext())
            {
                // entities
                Blog blog = new Blog();
                Post post = new Post();
                User user = new User();
                UserLogInAttributes attributes = new UserLogInAttributes();


                blog.Name = "Menaver's Blog";
                blog.Url = "www.someblog.com/blog/1";
                blog.Posts = new Post[1] { post };
                blog.Users = new User[1] { user };

                post.Title = "Awesome C#";
                post.Content = "C# is awesome!";

                user.Name = "Dmitry";
                user.UserLogInAttributes = attributes;
                user.Blogs = new Blog[1] { blog };

                attributes.Login = "******";
                attributes.Password = "******";


                db.Blogs.Add(blog);

                db.SaveChanges();



                // LinqToSql example
                var query = from b in db.Blogs
                            orderby b.Name
                            select b;
            }
        }
Beispiel #4
0
 public void Delete(Post entity)
 {
     UnitOfWork.Posts.Remove(entity);
     UnitOfWork.SaveChanges();
 }