Introduction
- {
- "dependencies": {
- "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
- "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
- },
- "frameworks": {
- "netcoreapp1.0": {
- "imports": ["dotnet5.6", "portable-net45+win8"]
- }
- },
- "tools": {
- "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
- },
- "version": "1.0.0-*"
- }
- using System;
- namespace GR.Data {
- public class BaseEntity {
- public Int64 Id {
- get;
- set;
- }
- public DateTime AddedDate {
- get;
- set;
- }
- public DateTime ModifiedDate {
- get;
- set;
- }
- public string IPAddress {
- get;
- set;
- }
- }
- }

Author-Book Relationship
- using System.Collections.Generic;
- namespace GR.Data {
- public class Author: BaseEntity {
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- public virtual ICollection < Book > Books {
- get;
- set;
- }
- }
- }
- using Microsoft.EntityFrameworkCore.Metadata.Builders;
- namespace GR.Data {
- public class AuthorMap {
- public AuthorMap(EntityTypeBuilder < Author > entityBuilder) {
- entityBuilder.HasKey(t => t.Id);
- entityBuilder.Property(t => t.FirstName).IsRequired();
- entityBuilder.Property(t => t.LastName).IsRequired();
- entityBuilder.Property(t => t.Email).IsRequired();
- }
- }
- }
- using System;
- namespace GR.Data {
- public class Book: BaseEntity {
- public Int64 AuthorId {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string ISBN {
- get;
- set;
- }
- public string Publisher {
- get;
- set;
- }
- public virtual Author Author {
- get;
- set;
- }
- }
- }
- using Microsoft.EntityFrameworkCore.Metadata.Builders;
- namespace GR.Data {
- public class BookMap {
- public BookMap(EntityTypeBuilder < Book > entityBuilder) {
- entityBuilder.HasKey(t => t.Id);
- entityBuilder.Property(t => t.Name).IsRequired();
- entityBuilder.Property(t => t.ISBN).IsRequired();
- entityBuilder.Property(t => t.Publisher).IsRequired();
- entityBuilder.HasOne(e => e.Author).WithMany(e => e.Books).HasForeignKey(e => e.AuthorId);
- }
- }
- }
- using Microsoft.EntityFrameworkCore;
- namespace GR.Data {
- public class ApplicationContext: DbContext {
- public ApplicationContext(DbContextOptions < ApplicationContext > options): base(options) {}
- protected override void OnModelCreating(ModelBuilder modelBuilder) {
- base.OnModelCreating(modelBuilder);
- new AuthorMap(modelBuilder.Entity < Author > ());
- new BookMap(modelBuilder.Entity < Book > ());
- }
- }
- }
- using System.Collections.Generic;
- namespace GR.Data {
- public interface IRepository < T > where T: BaseEntity {
- IEnumerable < T > GetAll();
- T Get(long id);
- void Insert(T entity);
- void Update(T entity);
- void Delete(T entity);
- }
- }
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace GR.Data {
- public class Repository < T > : IRepository < T > where T: BaseEntity {
- private readonly ApplicationContext context;
- private DbSet < T > entities;
- string errorMessage = string.Empty;
- public Repository(ApplicationContext context) {
- this.context = context;
- entities = context.Set < T > ();
- }
- public IEnumerable < T > GetAll() {
- return entities.AsEnumerable();
- }
- public T Get(long id) {
- return entities.SingleOrDefault(s => s.Id == id);
- }
- public void Insert(T entity) {
- if (entity == null) {
- throw new ArgumentNullException("entity");
- }
- entities.Add(entity);
- context.SaveChanges();
- }
- public void Update(T entity) {
- if (entity == null) {
- throw new ArgumentNullException("entity");
- }
- context.SaveChanges();
- }
- public void Delete(T entity) {
- if (entity == null) {
- throw new ArgumentNullException("entity");
- }
- entities.Remove(entity);
- context.SaveChanges();
- }
- }
- }
- public void ConfigureServices(IServiceCollection services) {
- // Add framework services.
- services.AddApplicationInsightsTelemetry(Configuration);
- services.AddMvc();
- services.AddDbContext < ApplicationContext > (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- services.AddScoped(typeof(IRepository < > ), typeof(Repository < > ));
- }
Here, the DefaultConnection is a connection string that defined inappsettings.json file as per the following code snippet.
- {
- "ConnectionStrings": {
- "DefaultConnection": "Data Source=DESKTOP-RG33QHE;Initial Catalog=GRepoDb;User ID=sa; Password=*****"
- },
- "ApplicationInsights": {
- "InstrumentationKey": ""
- },
- "Logging": {
- "IncludeScopes": false,
- "LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
- }
- }
- }
Now, we have configured settings to create a database, so we have time to create a database, using migration. We must choose the GR.Data project in the Package Manager console during the performance of the steps mentioned below.
- Tools –> NuGet Package Manager –> Package Manager Console
- Run PM> Add-Migration MyFirstMigration to scaffold a migration to create the initial set of tables for our model. If we receive an error stating the term ‘add-migration’ is not recognized as the name of a cmdlet, then close and reopen Visual Studio.
- Run PM> Update-Database to apply the new migration to the database. Because our database doesn’t exist yet, it will be created for us before the migration is applied.
Create Application User Interface
Now, we proceed to the controller. We create two controllers, where one is AuthorController and another is BookController under the Controllers folder of the Application.
These controllers have all ActionResult methods for each user interface of an operation. We create an IRepository interface instance, then we inject it in the controller's constructor to get its object. The following is a partial code snippet for the AuthorController in which the repository is injected, using constructor dependency injection.
- using GR.Data;
- using GR.Web.Models;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace GR.Web.Controllers {
- public class AuthorController: Controller {
- private IRepository < Author > repoAuthor;
- private IRepository < Book > repoBook;
- public AuthorController(IRepository < Author > repoAuthor, IRepository < Book > repoBook) {
- this.repoAuthor = repoAuthor;
- this.repoBook = repoBook;
- }
- }
- }
- namespace GR.Web.Models {
- public class AuthorListingViewModel {
- public long Id {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- public int TotalBooks {
- get;
- set;
- }
- }
- }
- [HttpGet]
- public IActionResult Index() {
- List < AuthorListingViewModel > model = new List < AuthorListingViewModel > ();
- repoAuthor.GetAll().ToList().ForEach(a => {
- AuthorListingViewModel author = new AuthorListingViewModel {
- Id = a.Id,
- Name = $ "{a.FirstName} {a.LastName}",
- Email = a.Email
- };
- author.TotalBooks = repoBook.GetAll().Count(x => x.AuthorId == a.Id);
- model.Add(author);
- });
- return View("Index", model);
- }
- @model IEnumerable < AuthorListingViewModel > @using GR.Web.Models
- @using GR.Web.Code < div class = "top-buffer" > < /div> < div class = "panel panel-primary" > < div class = "panel-heading panel-head" > Authors < /div> < div class = "panel-body" > < div class = "btn-group" > < a id = "createEditAuthorModal"
- data - toggle = "modal"
- asp - action = "AddAuthor"
- data - target = "#modal-action-author"
- class = "btn btn-primary" > < i class = "glyphicon glyphicon-plus" > < /i> Add Author < /a> < /div> < div class = "top-buffer" > < /div> < table class = "table table-bordered table-striped table-condensed" > < thead > < tr > < th > Name < /th> < th > Email < /th> < th > Total Books < /th> < th > Action < /th> < /tr> < /thead> < tbody > @foreach(var item in Model) { < tr > < td > @Html.DisplayFor(modelItem => item.Name) < /td> < td > @Html.DisplayFor(modelItem => item.Email) < /td> < td > @Html.DisplayFor(modelItem => item.TotalBooks) < /td> < td > < a id = "editAuthorModal"
- data - toggle = "modal"
- asp - action = "EditAuthor"
- asp - route - id = "@item.Id"
- data - target = "#modal-action-author"
- class = "btn btn-info" > < i class = "glyphicon glyphicon-pencil" > < /i> Edit < /a> < a id = "addBookModal"
- data - toggle = "modal"
- asp - action = "AddBook"
- asp - route - id = "@item.Id"
- data - target = "#modal-action-author"
- class = "btn btn-success" > < i class = "glyphicon glyphicon-book" > < /i> Book < /a> < /td> < /tr>
- } < /tbody> < /table> < /div> < /div>
- @Html.Partial("_Modal", new BootstrapModel {
- ID = "modal-action-author", AreaLabeledId = "modal-action-author-label", Size = ModalSize.Large
- })
- @section scripts { < script src = "~/js/author-index.js"
- asp - append - version = "true" > < /script>
- }
- It shows all forms in bootstrap model popup so create the author - index.js file as per following code snippet.
- (function($) {
- function Author() {
- var $this = this;
- function initilizeModel() {
- $("#modal-action-author").on('loaded.bs.modal', function(e) {}).on('hidden.bs.modal', function(e) {
- $(this).removeData('bs.modal');
- });
- }
- $this.init = function() {
- initilizeModel();
- }
- }
- $(function() {
- var self = new Author();
- self.init();
- })
- }(jQuery))

Author Listing
- using System.ComponentModel.DataAnnotations;
- namespace GR.Web.Models {
- public class AuthorBookViewModel {
- public long Id {
- get;
- set;
- }
- [Display(Name = "First Name")]
- public string FirstName {
- get;
- set;
- }
- [Display(Name = "Last Name")]
- public string LastName {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- [Display(Name = "Book Name")]
- public string BookName {
- get;
- set;
- }
- public string ISBN {
- get;
- set;
- }
- public string Publisher {
- get;
- set;
- }
- }
- }
- [HttpGet]
- public PartialViewResult AddAuthor() {
- AuthorBookViewModel model = new AuthorBookViewModel();
- return PartialView("_AddAuthor", model);
- }
- [HttpPost]
- public ActionResult AddAuthor(AuthorBookViewModel model) {
- Author author = new Author {
- FirstName = model.FirstName,
- LastName = model.LastName,
- Email = model.Email,
- AddedDate = DateTime.UtcNow,
- IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(),
- ModifiedDate = DateTime.UtcNow,
- Books = new List < Book > {
- new Book {
- Name = model.BookName,
- ISBN = model.ISBN,
- Publisher = model.Publisher,
- IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(),
- AddedDate = DateTime.UtcNow,
- ModifiedDate = DateTime.UtcNow
- }
- }
- };
- repoAuthor.Insert(author);
- return RedirectToAction("Index");
- }
- @model AuthorBookViewModel
- @using GR.Web.Models < form asp - action = "AddAuthor"
- role = "form" > @await Html.PartialAsync("_ModalHeader", new ModalHeader {
- Heading = "Add Author"
- }) < div class = "modal-body form-horizontal" > < div class = "row" > < div class = "col-lg-6" > < div class = "form-group" > < label asp -
- for = "FirstName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "FirstName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "LastName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "LastName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "Email"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "Email"
- class = "form-control" / > < /div> < /div> < /div> < div class = "col-lg-6" > < div class = "form-group" > < label asp -
- for = "BookName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "BookName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "ISBN"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "ISBN"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "Publisher"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "Publisher"
- class = "form-control" / > < /div> < /div> < /div> < /div> < /div>
- @await Html.PartialAsync("_ModalFooter", new ModalFooter {}) < /form>

Add Author and Book Screen
- using System.ComponentModel.DataAnnotations;
- namespace GR.Web.Models {
- public class AuthorViewModel {
- [Display(Name = "First Name")]
- public string FirstName {
- get;
- set;
- }
- [Display(Name = "Last Name")]
- public string LastName {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- }
- }
- [HttpGet]
- public IActionResult EditAuthor(long id) {
- AuthorViewModel model = new AuthorViewModel();
- Author author = repoAuthor.Get(id);
- if (author != null) {
- model.FirstName = author.FirstName;
- model.LastName = author.LastName;
- model.Email = author.Email;
- }
- return PartialView("_EditAuthor", model);
- }
- [HttpPost]
- public IActionResult EditAuthor(long id, AuthorViewModel model) {
- Author author = repoAuthor.Get(id);
- if (author != null) {
- author.FirstName = model.FirstName;
- author.LastName = model.LastName;
- author.Email = model.Email;
- author.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
- author.ModifiedDate = DateTime.UtcNow;
- repoAuthor.Update(author);
- }
- return RedirectToAction("Index");
- }
- @model AuthorViewModel
- @using GR.Web.Models < form asp - action = "EditAuthor"
- role = "form" > @await Html.PartialAsync("_ModalHeader", new ModalHeader {
- Heading = "Edit Author"
- }) < div class = "modal-body form-horizontal" > < div class = "form-group" > < label asp -
- for = "FirstName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "FirstName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "LastName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "LastName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "Email"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "Email"
- class = "form-control" / > < /div> < /div> < /div>
- @await Html.PartialAsync("_ModalFooter", new ModalFooter {}) < /form>

Edit Author
- using System.ComponentModel.DataAnnotations;
- namespace GR.Web.Models {
- public class BookViewModel {
- [Display(Name = "Book Name")]
- public string BookName {
- get;
- set;
- }
- public string ISBN {
- get;
- set;
- }
- public string Publisher {
- get;
- set;
- }
- }
- }
- [HttpGet]
- public PartialViewResult AddBook(long id) {
- BookViewModel model = new BookViewModel();
- return PartialView("_AddBook", model);
- }
- [HttpPost]
- public IActionResult AddBook(long id, BookViewModel model) {
- Book book = new Book {
- AuthorId = id,
- Name = model.BookName,
- ISBN = model.ISBN,
- Publisher = model.Publisher,
- IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString(),
- AddedDate = DateTime.UtcNow,
- ModifiedDate = DateTime.UtcNow
- };
- repoBook.Insert(book);
- return RedirectToAction("Index");
- }
- @model BookViewModel
- @using GR.Web.Models < form asp - action = "AddBook"
- role = "form" > @await Html.PartialAsync("_ModalHeader", new ModalHeader {
- Heading = "Add Book"
- }) < div class = "modal-body form-horizontal" > < div class = "form-group" > < label asp -
- for = "BookName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "BookName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "ISBN"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "ISBN"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "Publisher"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "Publisher"
- class = "form-control" / > < /div> < /div> < /div>
- @await Html.PartialAsync("_ModalFooter", new ModalFooter {}) < /form>

Add Book
- sing System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.AspNetCore.Mvc;
- using GR.Data;
- using GR.Web.Models;
- using Microsoft.AspNetCore.Mvc.Rendering;
- using Microsoft.AspNetCore.Http;
- namespace GR.Web.Controllers {
- public class BookController: Controller {
- private IRepository < Author > repoAuthor;
- private IRepository < Book > repoBook;
- public BookController(IRepository < Author > repoAuthor, IRepository < Book > repoBook) {
- this.repoAuthor = repoAuthor;
- this.repoBook = repoBook;
- }
- }
- }
- namespace GR.Web.Models {
- public class BookListingViewModel {
- public long Id {
- get;
- set;
- }
- public string BookName {
- get;
- set;
- }
- public string AuthorName {
- get;
- set;
- }
- public string ISBN {
- get;
- set;
- }
- public string Publisher {
- get;
- set;
- }
- }
- }
- public IActionResult Index() {
- List < BookListingViewModel > model = new List < BookListingViewModel > ();
- repoBook.GetAll().ToList().ForEach(b => {
- BookListingViewModel book = new BookListingViewModel {
- Id = b.Id,
- BookName = b.Name,
- Publisher = b.Publisher,
- ISBN = b.ISBN
- };
- Author author = repoAuthor.Get(b.AuthorId);
- book.AuthorName = $ "{author.FirstName} {author.LastName}";
- model.Add(book);
- });
- return View("Index", model);
- }
- @model IEnumerable < BookListingViewModel > @using GR.Web.Models
- @using GR.Web.Code < div class = "top-buffer" > < /div> < div class = "panel panel-primary" > < div class = "panel-heading panel-head" > Books < /div> < div class = "panel-body" > < div class = "top-buffer" > < /div> < table class = "table table-bordered table-striped table-condensed" > < thead > < tr > < th > Name < /th> < th > Author Name < /th> < th > ISBN < /th> < th > Publisher < /th> < th > Action < /th> < /tr> < /thead> < tbody > @foreach(var item in Model) { < tr > < td > @Html.DisplayFor(modelItem => item.BookName) < /td> < td > @Html.DisplayFor(modelItem => item.AuthorName) < /td> < td > @Html.DisplayFor(modelItem => item.ISBN) < /td> < td > @Html.DisplayFor(modelItem => item.Publisher) < /td> < td > < a id = "editBookModal"
- data - toggle = "modal"
- asp - action = "EditBook"
- asp - route - id = "@item.Id"
- data - target = "#modal-action-book"
- class = "btn btn-info" > < i class = "glyphicon glyphicon-pencil" > < /i> Edit < /a> < a id = "deleteBookModal"
- data - toggle = "modal"
- asp - action = "DeleteBook"
- asp - route - id = "@item.Id"
- data - target = "#modal-action-book"
- class = "btn btn-danger" > < i class = "glyphicon glyphicon-trash" > < /i> Delete < /a> < /td> < /tr>
- } < /tbody> < /table> < /div> < /div>
- @Html.Partial("_Modal", new BootstrapModel {
- ID = "modal-action-book", AreaLabeledId = "modal-action-book-label", Size = ModalSize.Medium
- })
- @section scripts { < script src = "~/js/book-index.js"
- asp - append - version = "true" > < /script>
- }
- (function($) {
- function Book() {
- var $this = this;
- function initilizeModel() {
- $("#modal-action-book").on('loaded.bs.modal', function(e) {}).on('hidden.bs.modal', function(e) {
- $(this).removeData('bs.modal');
- });
- }
- $this.init = function() {
- initilizeModel();
- }
- }
- $(function() {
- var self = new Book();
- self.init();
- })
- }(jQuery))

Book Listing
- using Microsoft.AspNetCore.Mvc.Rendering;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- namespace GR.Web.Models {
- public class EditBookViewModel {
- [Display(Name = "Book Name")]
- public string BookName {
- get;
- set;
- }
- public string ISBN {
- get;
- set;
- }
- public string Publisher {
- get;
- set;
- }
- public List < SelectListItem > Authors {
- get;
- set;
- } = new List < SelectListItem > ();
- [Display(Name = "Author")]
- public long AuthorId {
- get;
- set;
- }
- }
- }
- public PartialViewResult EditBook(long id) {
- EditBookViewModel model = new EditBookViewModel();
- model.Authors = repoAuthor.GetAll().Select(a => new SelectListItem {
- Text = $ "{a.FirstName} {a.LastName}",
- Value = a.Id.ToString()
- }).ToList();
- Book book = repoBook.Get(id);
- if (book != null) {
- model.BookName = book.Name;
- model.ISBN = book.ISBN;
- model.Publisher = book.Publisher;
- model.AuthorId = book.AuthorId;
- }
- return PartialView("_EditBook", model);
- }
- [HttpPost]
- public ActionResult EditBook(long id, EditBookViewModel model) {
- Book book = repoBook.Get(id);
- if (book != null) {
- book.Name = model.BookName;
- book.ISBN = model.ISBN;
- book.Publisher = model.Publisher;
- book.AuthorId = model.AuthorId;
- book.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();
- book.ModifiedDate = DateTime.UtcNow;
- repoBook.Update(book);
- }
- return RedirectToAction("Index");
- }
- @model EditBookViewModel
- @using GR.Web.Models < form asp - action = "EditBook"
- role = "form" > @await Html.PartialAsync("_ModalHeader", new ModalHeader {
- Heading = "Edit Book"
- }) < div class = "modal-body form-horizontal" > < div class = "form-group" > < label asp -
- for = "BookName"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "BookName"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "ISBN"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "ISBN"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "Publisher"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < input asp -
- for = "Publisher"
- class = "form-control" / > < /div> < /div> < div class = "form-group" > < label asp -
- for = "AuthorId"
- class = "col-lg-3 col-sm-3 control-label" > < /label> < div class = "col-lg-6" > < select asp -
- for = "AuthorId"
- asp - items = "@Model.Authors"
- class = "form-control" > < option > Please select < /option> < /select> < /div> < /div> < /div>
- @await Html.PartialAsync("_ModalFooter", new ModalFooter {}) < /form>

Edit Book
- [HttpGet]
- public PartialViewResult DeleteBook(long id) {
- Book book = repoBook.Get(id);
- return PartialView("_DeleteBook", book ? .Name);
- }
- [HttpPost]
- public ActionResult DeleteBook(long id, FormCollection form) {
- Book book = repoBook.Get(id);
- if (book != null) {
- repoBook.Delete(book);
- }
- return RedirectToAction("Index");
- }
- The GET request
- for the DeleteBook action method returns _DeleteBook partial view which code snippet is following under the Book folder of Views.
- @model string
- @using GR.Web.Models < form asp - action = "DeleteBook"
- role = "form" > @Html.Partial("_ModalHeader", new ModalHeader {
- Heading = "Delete Book"
- }) < div class = "modal-body form-horizontal" > Are you want to delete @Model ? < /div>
- @Html.Partial("_ModalFooter", new ModalFooter {
- SubmitButtonText = "Delete"
- }) < /form>

Delete Book
- Generic Repository Pattern in ASP.NET Core
- Repository Pattern in ASP.NET Core
- CRUD Operations in ASP.NET Core and Entity Framework Core
- Rating Star Application in ASP.NET Core
See Also
Its recommended reading more articles related to ASP.NET Core.
- Overview Of ASP.NET Core
- ASP.NET Core With Visual Studio 2017 RC
- CRUD Operations In ASP.NET Core using Entity Framework Core Code First
- Repository Pattern In ASP.NET Core
- Generic Repository Pattern In ASP.NET Core

reza akhlaghiPosted Feb 22, 2023, 12:43 PM
Tanx for your simple and good Descriptions :)
Felix DuquePosted Feb 22, 2021, 2:46 AM
I am no expert, but I would not recommend doing the following:author.TotalBooks = repoBook.GetAll().Count(x => x.AuthorId == a.Id);This will load all of the books in memory and then count based on the query. My recommendation is to create a repository per domain, and then from your book repository you could have a GetAllBooksByAuthor(int authorid) and there you could do the same linq query with the count, and it will perform a Count on the sql server, instead of doing a select * from the table with no parameter, and then doing the count. Anyways that’s my two cents. I hope this comment helps someone.
rtfm plizPosted Nov 8, 2020, 3:28 AM
Because I can't download the source, I tried to make this. but still error on Deleteing Book, you cant get from https://github.com/rtfmpliz/GR
Guest UserPosted Mar 24, 2019, 11:25 AM
So much useful. Thanks
mike kielochPosted Jan 28, 2017, 1:12 AM
Question. How do you feel about injecting a single repository object into your service and then for each of your interface methods inside your repository call _context.Set<T>() instead of creating the set during construction of your repository?
Former memberPosted Jan 23, 2017, 4:20 AM
You published one article on onion architecture and there i asked one question that onion architecture is same as domain driven design pattern ? please discuss it in details. thanks
Former memberPosted Jan 23, 2017, 4:18 AM
You said : we must not call store procedure from repository pattern, You should create a method which returns context. That context can be used to call stored procedure in the application. so would you please post a code sample which guide me how to do it with repository design pattern
Catalin PopPosted Jan 20, 2017, 4:33 AM
There are several "Grave" mistakes in this article:1. public IEnumerable < T > GetAll() { return entities.AsEnumerable(); } This will force in memory evaluation of all queries, basically loading the entire table on each call.(Please revisit Linq and read about IEnumerable vs IQueryable) 2. "context.SaveChanges(); in each Insert, Update, Delete method." This is a grave design mistake, it does not allow you to work with Graphs o entities and completly elimitates the capability of EF for example to order operations and perform atomic transactions on save. (And EF Core does not support ambient transactions). 3. Base entity Another design with problems, it does not respect separation of concerns and constraints database design. Points for effort, but you should really eliminate these design errors before publishing such an article.
Former memberPosted Jan 20, 2017, 3:26 AM
How to add more method in Repository class which allow user to pass store procedure name and params collection because many time we use store procedure instead of inline sql. please looking for your suggestion.
BettyPosted Jan 20, 2017, 12:54 AM
This blogpost feels like it could do with a different title. The repository pattern is so basic and such a small part of it it really doesn't seem like the article focus. That said I think the repository pattern generally foes more harm than good, abstracting 2 lines of code isn't worth an abstraction, especially when one of the lines is save changes which shouldn't be in the method in the first place. What happens if I want to add multiple items at the same time? Performance nightmare waiting to happen.
Dave SolenovexPosted Jan 19, 2017, 7:31 AM
Hi Sandeep, I'm just a beginner for asp.net core. May I ask about what should I do if I want to Update 2 type of Models in one Submit? should I Implement the Unit of Work Pattern ?
Manju lata YadavPosted Dec 19, 2016, 6:33 AM
Nice article for starting with ASP.NET Core and Entity Framework Core.
Henry WolfePosted Dec 9, 2016, 10:57 AM
I would rate this article a 3 out of 5. You can't follow and actually step through it. I started from scratch and tried to implement this project but I could not make it work because there are to many holes.
Debendra DashPosted Dec 5, 2016, 1:17 AM
Good to know about generic repository.........
Mahesh ChandPosted Dec 1, 2016, 7:36 PM
Good one, Sandeep. Well written!