Posts

Showing posts with the label MVC

CRUD operation using entity framework in .net core MVC application

Image
In Previous article " Setup Code first approach in .net core MVC application using entity framework " we have discussed setting up the code first approach in .net core application. We have created our first model in the sample project and we will be using the same project in this article. In this article we will doing the CRUD operation using entity framework in .net core MVC application. Visual studio provides scaffolding template for these operation but we will not use the scaffolding template for the CRUD operation rather we use the manual custom code for understating of the subject better.       Lets start the implementation. Before we start the crud operation activity we need to do some useful changes to entity migration. In previous article we have used the update-database command to migrate changes to database, but in real time web application this approach would not work so for that we need to make changes such that the migration changes will commit to database with

Setup Code first approach in .net core MVC application using entity framework

Image
In Previous article " .Net core MVC project structure explained step by step part 2 " we have discussed about .Net core MVC project structure. In this article we will be setting up the code first approach in .net core application. To control  and manage the entities we can opt for code first approach to design our database structure. To setup the entity framework in .net core app we need to add following nuget package to our projects. Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Relational Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design       I will show you how and where you need to use the above packages to setup the complete structure. I will use the same solution which i have used in previous article to setup the code first approach. Lets start the implementation. Go to solution explorer and right click on solution then click on new project then create the class library project and name it as

.Net core MVC project structure explained step by step part 2

Image
 In previous article " Net core MVC project structure explained step by step part 1 " I have discussed about creating MVC .net core project and its basic structure. In this article i will discuss MVC .net core project structure further.       Following is the project structure which i will be discussing in this article.     wwwroot: This is the directory where we can store projects static files for example css files, Javascript files, images, or any other binary files required for the project. Controllers: We can add the controllers of the project. Controllers are responsible for handling user request, do the processing and provide the appropriate response to user request.  Models: you can store your model files and dbcontext in this folder to add your business logic in application. Views: In this directory we can add views in  the form of chtml files(Razor engine)  appsetting.json: In this file we can configure the custom attributes required for the project for fine tuni

.Net core MVC project structure explained step by step part 1

Image
 To understand the .net core project structure we need to create the new MVC application. for that go to file menu and click on create a new project menu. select the Asp .net core web app template from the popup window as shown below.         Then click on next button and provide application name you want to create the project for and select the appropriate location where you want to create the project and click on next button as shown in below image. in next screen you can select the target framework and click on create button as shown below image.  Now right click on solution explorer and click on manage nuget package option and add any package you are going to use in the application. In my case i have installed the Nlog package in the sample project. below is the glimpse  of my solution in the image. so in solution explorer above you can see that there is dependencies section where following are the project dependency categories. Frameworks: The framework category contain the infor

Dependency injection in ASP.NET Core MVC controller explained step by step

ASP.NET Core MVC controllers should request their dependencies explicitly via their constructors. In some instances, individual controller actions may require a service, and it may not make sense to request at the controller level. In this case, you can also choose to inject a service as a parameter on the action method. With ASP.NET 5 MVC6, we get a feature called dependency injection baked right into the framework for us. Dependency injection is a technique that follows the Dependency Inversion Principle, allowing for applications to be composed of loosely coupled modules. ASP.NET Core has built-in support for dependency injection, which makes applications easier to test and maintain. ASP.NET Core's built-in support for constructor-based dependency injection extends to MVC controllers. By simply adding a service type to your controller as a constructor parameter, ASP.NET Core will attempt to resolve that type using its built in service container. Services are typically, b

Introduction of Asp.net MVC Basics for beginners

MVC is an architectural pattern which separates the representation and user interaction. It's divided into three broader sections, Model View Controller.  Below is how each one of them handles the task. View : The View is responsible for the look and feel. Model: Model represents the real world object and provides data to the View. Controller: The Controller is responsible for taking the end user request and loading the appropriate Model and View. Advantages of ASP.NET MVC Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested. Complex applications can be easily managed Separation of concerns. Different aspects of the application can be divided into Model, View and Controller. ASP.NET MVC views are light weight, as they do not use view-state. It provides extensive support for URL Routing that helps to make friendly URLs (means friendly for human as well as Search Engines). Support for existing ASP.NET features like membership