Entity Framework Handson solutions cognizant

BookEntry_FluentAPI Entity Framework Cognizant Handson

In this post, we are going to cover BookEntry_FluentAPI Handson which is asked under the Entity Framework Topic in Cognizant Solutions.

Building model Code first Approach, Fluent Api, Querying data using LINQ

BookEntry_FluentAPI

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Personnal Comment If You Leave This File As It & Try To 
// Evaluate it will successfully evaluated

namespace Exercise1   //DO NOT change the namespace name
{
    public class Program   //DO NOT change the class name
    {
        static void Main(string[] args)      //DO NOT change the 'Main' method signature
        {
            //Implement code here
        }
    }
}

Book.cs

//THIS IS FOR REFERENCE ONLY. YOU ARE NOT REQUIRED TO MAKE ANY CHANGES HERE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise1
{
    public class Book
    {
        public int BookId { get; set; }
        public String BookName { get; set; }
        public String BookAuthor { get; set; }
        public String BookGenre { get; set; }
        public double BookPrice { get; set; }
    }
}

Also Read : StudentDetails Using CodeFirst Entity Framework Handson Solution Cognizant

BookUtil.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise1              //DO NOT change the namespace name
{
    public class BookUtil         //DO NOT change the class name 
    {

        public Book AddBook(Book book)    //DO NOT change the method Name and Signature
        {
            using (var db = new LibraryContext())
            {
                var b = db.Books.Add(book);
                db.SaveChanges();
                return b;
            }
        }


        public List<Book> GetBookByGenre(String Genre)   //DO NOT change the method Name and Signature
        {
            using (var db = new LibraryContext())
            {
                var books = db.Books.Where(b => b.BookGenre == Genre);
                return books.ToList();
            }
        }

        public List<Book> GetBooksList() //DO NOT change the method Name and Signature
        {
            using (var db = new LibraryContext())
            {
                var books = db.Books;
                return books.ToList();
            }

        }
        public Book UpdateBookPrice(int NewPrice, int Bookid)   //DO NOT change the method Name and Signature
        {
            using (var db = new LibraryContext())
            {
                var book = db.Books.Single(b => b.BookId == Bookid);
                book.BookPrice = NewPrice;
                db.SaveChanges();
                return book;
            }

        }

        public Book DeleteBook(int BookId)  //DO NOT change the method Name and Signature
        {
            using (var db = new LibraryContext())
            {
                var book = db.Books.Single(b => b.BookId == BookId);
                var delBooks = db.Books.Remove(book);
                db.SaveChanges();
                return delBooks;
            }
        }

    }
}

LibraryContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace Exercise1                      //DO NOT change the namespace name
{
    class LibraryContext : DbContext
    {
        public DbSet<Book> Books { get; set; }

        public LibraryContext() : base("name=BookStore") { }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder
                .Entity<Book>()
                .ToTable("BookDetail");

            modelBuilder
                .Entity<Book>()
                .Property(b => b.BookId)
                .HasColumnName("Book_Id");
        }
    }
}

Similar Posts