Posts

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

Configure archival and purging of log file in Nlog

Image
  In previous article " Implement Logging in Sql Server Database using Nlog in .net core MVC application - part 3 "  I have demonstrated that how we can implement Logging in sql server database using Nlog in .core mvc application. In previous article I have logged the exceptions in sql server database , in this article I will show you how you can configure archival and purging of log file by modifying the few settings in Nlog.config file. Archival and purging of logs will help us to remove the outdated log files from the server and keep server storage junk free. Lets configure the Nlog.config file to achieve the archival and purging of log file. <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true&qu

Implement Logging in Sql Server Database using Nlog in .net core MVC application - part 3

Image
In previous article " Implement Logging in CSV file using Nlog in .net core MVC application- part 2 "  I have demonstrated that how we can implement Logging in CSV file using Nlog in .core mvc application. In previous article I have logged the exceptions in CSV file, in this article I will show you how you can store the exception and custom logs in SQL server database using Nlog in .net core MVC application. Storing logs in SQL server database can help us to query the exception data efficiently and we can show it to UI if needed to check exception upfront.      Lets start the implementation of it, I going to use the same example project used in previous article/demonstration. The following nuget package needs to be installed before implement the sql server logging using NLog.   "Microsoft.Data.SqlClient" Version="3.0.1" "NLog" Version="4.7.13"  "NLog.Config" Version="4.7.13" "NLog.Extensions.Logging" Versio