Subscribe Us

LightBlog

Thursday, 27 August 2020

August 27, 2020

Web API 2 With Entity Framework 6 Code First Migrations

In this article, i will show you, crud operation code first approach in ASP.NET MVC entity framework:

1. I am using Visual Studio 2017. Open the Visual Studio and add a new project.

2.Choose the “Web” option in installed templates and choose “ASP.NET Web Application (.NET Framework)”. Give the proper name of project. and select .net framework. Then click on OK.

3.When you click OK, you’ll be prompted to choose the type of ASP.NET Web Application. Choose Web API and click OK.


  • Install this package from Tools menu-> nuget package manger-> Package manager console-> Install-Package EntityFramework
4.Right-click Models folder and add a new class. Name the class as “Student”.
5.Make the class public and add two properties to the class, i.e., Id and Name. Id will serve as     a primary key to this entity.

 public class Student  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
    }  
6.Rebuild the solution.
7. Right click the controller folder and add choose the option to add a new controller class.
8. In the next prompt, choose the option to create a Web API 2 Controller with actions, using        Entity Framework. Click on Add button.
9. Next, choose the model we created i.e. Student model in the option of Model class.
10. Since we do not have data context for our application, click on the + button close to Data          context class option dropdown, and provide the name “StudentManagementContext” in            the text box shown and click Add.
11.The name of the controller should be “StudentsController”. Click Add to finish.
12. In that controller scaffolding template will provide you the method of CRUD operation.

Entity Framework Code First Migrations

  • Open Package Manager Console and select the default project as your WebAPI project. Type the command Enable-Migrations and press enter.
  • Once the command is executed, it does some changes to our solution. As a part of adding migrations, it creates a Migrations folder and adds a class file named ”Configuration.cs”. This class is derived from DbMigrationsConfiguration class. 
  • The context parameter is the instance of our context class that got generated while we were adding a controller. We provided the name as StudentManagementContext. This class derives from DbContext class. This context class takes care of DB schema and the DbSet properties of this class are basically the tables that we’ll have when our database will be created. It added Students as a DbSet property that returns our Student model/entity and would be directly mapped to the table that will be generated in the database.
  • The next step is to execute the command named “Add-Migrations”. In the package manager console, execute this command with a parameter of your choice that would be the name of our first migration. I call it ”Initial”. So, the command would be Add-Migrations Initial.
  • Again in the package manager console, run the command “Update-Database”.

Let’s see what we got in our database when the earlier command got successfully executed.

  1. Since we used the local database, we can open it by opening Server Explorer from the View tab in Visual Studio itself.
  2. Once the Server Explorer is shown, we can find the StudentManagementContext database generated and it has two tables named Students and __MigrationHistory. Students table corresponds to our Student model in the code base and __MigrationsHistory table is the auto-generated table that keeps track of the executed migrations.

Monday, 10 August 2020

August 10, 2020

Using command prompt or Git Bash publish code or files on GitLab

In this article, I am going to show, how you can push your all code into a code repository.

Create a project in GitLab

  • Goto https://gitlab.com/  Create your account follow this steps,
  • In your dashboard, click the green New project button or use the plus icon in the navigation bar. This opens the New project page
  • On the New project page, we choose  Create a blank project.

Blank projects:

  • To create a new blank project on the New project page:
  • On the Blank project tab, provide the following information:
  • The name of your project in the Project name field. You can’t use special characters, but you can use spaces, hyphens, underscores or even emoji. When adding the name, the Project slug will auto populate. The slug is what the GitLab instance will use as the URL path to the project. If you want a different slug, input the project name first, then change the slug after.
  • The path to your project in the Project slug field. This is the URL path for your project that the GitLab instance will use. If the Project name is blank, it will auto populate when you fill in the Project slug.
  • The Project description (optional) field enables you to enter a description for your project’s dashboard, which will help others understand what your project is about. Though it’s not required, it’s a good idea to fill this in.
  • Changing the Visibility Level modifies the project’s viewing and access rights for users.
  • Selecting the Initialize repository with a README option creates a README file so that the Git repository is initialized, has a default branch, and can be cloned.
  • Click Create project.

Step 1: Download git from https://git-scm.com/ and install git 

Step 2: Check git is installed on your system

  git --version

Step 3: Run following git commands

   git config --global user.name “xxxxx”

   git config --global user.name

   git config --global user.email “xxxxx”

   git config --global user.email

   git config --global --list

Step 4: Create a demo project/folder & add to git 

Step 5: Goto cmd OR terminal OR git bash 

   CD to the location of the folder and run following commands

   git init

   git status

   git add .

   git commit -m “msg” (commit your changes)

   git push -u “url” master (push your change into GitLab)

Step 6: Check project (files) added on GitLab