0
/
0

Welcome to Thrill of .NET Core

An easy guidline to start coding in .NET Core

Instructors

Shunjid Rahman Showrov
Programmer, Open source contributor

Batch 22

Department of Software Engineering

Daffodil International University


Zubayer Himel
Programmer, Open source contributor

Batch 22

Department of Software Engineering

Daffodil International University


Table of contents

Installing environment in VSCode

Download VSCode

Visual Studio Code (VSCode) is a source code editor developed by Microsoft for Windows, Linux, macOS. It is free to download. You can download it from its official website. Download Link


Setup extension in VSCode

After successful installation click on widgets this icon from the left bar or simply press Ctrl+Shift+X to go to extensions. In search bar type C# and an extension will appear. Install that extension. This extension will provide the following features inside VSCode -

  • Lightweight development tools for .NET Core
  • Great C# editing support including Syntax Highlighting, IntelliSense, Go to Definition, Find All References etc.
  • Debugging support for .NET Core (CoreCLR)
  • Support for project.json and csproj projects on Windows, Linux and macOS

You can also install another extension named as C# Extension. It will provide extensions to the IDE that will hopefully speed up your development workflow. Now you are good to go.

Project structure

Create a Project

Open the terminal from the folder or directory in which you want to create the project. Lets say you want to create a project named musicPlayer. The following commnad will create a project for you. dotnet new mvc -o musicPlayer Then you need to change your directory to that project and open it in VSCode. After opening the project you will see a notification on the right bottom corner. Click on Yes button.

Project structure

  • .csproj file
  • Model
  • View
  • Controller
  • wwwroot

Creating models

Model represents as database tables. There are two approach to make a project. One is database first approach and another is code first approach. Model creating is code first approach. Every data type of model is an entity. You can declare variable type, length, auto increment id, foreign key etc.
To create a model click new c# class from Model folder. A file will be creted in model folder with .cs extension. Example: abc.cs

Data annotation

What is Data Annotation?

Data annotations are nothing but certain validations that we put in our models to validate the input from the user. ASP.NET MVC provides a unique feature in which we can validate the models using Data annotation attribute. Import the following namespace to use data annotation in the application. System.ComponentModel.DataAnnotations It is very easy to use and the code becomes much cleaner as compared to normal ASP.NET validators.

Types of data annotation
Data annotation Description Syntax
Required This attribute specifies that the value is mandatory and cannot be skipped. [Required(ErrorMessage="Please enter name"),MaxLength(30)]
DataType This attribute is used to specify the datatype of the model. [DataType(DataType.Text)]
Range Using this attribute we can set a range between two numbers. [Range(100,500,ErrorMessage="Please enter correct value")]
StringLength Using this attribute we can specify maximum and minimum length of the property. [StringLength(30,ErrorMessage="Do not enter more than 30 characters")]
DisplayName Using this attribute we can specify property name to be displayed on view. [Display(Name="Student Name")]
MaxLength Using this attribute we can specify maximum length of property. [MaxLength(3)]
Bind This attribute specify fields to include or exclude for model binding. [Bind(Exclude = "StudentID")]
DisplayFormat This attribute allows us to set date in the format specified as per the attribute. [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
RegularExpression We can set a regex pattern for the property. For ex: Email ID. [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email is not valid.")]

Scaffolding from CLI

Why Scaffold?

Use the scaffolding tool to produce Create, Read, Update and Delete (CRUD) pages.


Scaffold code and parameters

dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

You need to run this command for every Model of your project and every time you need to change model name and controller name from the command line. It can be a pain if you have multiple models. You can use this Link to generate scaffold command.
Parameter Description
-m The name of the model
-dc The data context
-udl Use the default layout
-relativeFolderPath The relative output folder path to create the files
-useDefaultLayout The default layout should be used for the views
-referenceScriptLibraries Adds _ValidationScriptPartial to Edit and Create pages

Database Migration

After successful scaffolding you need EF Core Migration to create database. Migration is a set of tools that let you create and update a database to match your data model. For migration use the following commands - dotnet ef migrations add InitialCreate dotnet ef database update

  • ef migrations add InitialCreate: Generates an Migrations/{timestamp}_InitialCreate.cs migration file. The InitialCreate argument is the migration name. Any name can be used, but by convention, a name is selected that describes the migration. Because this is the first migration, the generated class contains code to create the database schema. The database schema is based on the model specified in the MvcMovieContext class (in the Data/MvcMovieContext.cs file).

  • ef database update:

    Updates the database to the latest migration, which the previous command created. This command runs the Up method in the Migrations/{time-stamp}_InitialCreate.cs file, which creates the database.

keyboard_arrow_up
keyboard_arrow_down