- We are going to build a net website using .Net core template on Mac (steps could be same for Linux/Unix)
- We will be using MSSql 2017 hosted in a docker container.
- MSSql on docker is just for development, for production you can use Azure DB hosted on Azure or simply deploy docker in Azure containers (I never tried that - possible next article :) ).
Once installed let's test if docker works fine on your machine. To check, open your terminal and run following command
docker --version
Docker version 18.09.2, build 6247962
After installing we need to start docker application before moving forward. You can do this by Docker GUI or by starting it from cli. Following command is tested in Mac only:
open --background -a Docker
It will take a while to docker to start and be ready to take further commands
Now as we successfully start docker, Let's get the latest microsoft mssql image into our docker: 2017 sql server
docker pull microsoft/mssql-server-linux:2017-latest
Screenshot-docker_pull_sqlserver2017
- run: It creates a container and execute it.
- name: an alias for container. we will use it to start and stop the container
- e: it's a KEY=VAL pair to set environment variables
- p: It publish container's port to the host
- d: it runs container in detached mode i.e. in background
docker run \
-e 'ACCEPT_EULA=Y' \
-e 'MSSQL_SA_PASSWORD=Strong!Passw0rd' \
-p 1401:1433 \
--name sql2017 \
-d microsoft/mssql-server-linux:2017-latest
Screenshot-docker_run_sqlserver2017
Let's set our docker container to interactive mode so that we can run some bash commands like sqlcmd which are available in our sql2017 container
sudo docker exec -it sql2017 "bash"
Screenshot-docker-exec-bash
then it will ask for your machine password. Type the password and hit enter and now you are in interactive mode
Now we need to run sql commands. We will use sqlcmd cli. To do that we will create a sql session so that we can fire sql commands
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA
Screenshot-sql-cmd-prompt
Let's create a new Database called MySampleDB. It won't run following query immediatly to run it you have to type 'GO' in next line. Let's create database and then list all databases exist in current instance of SQL
Create Database MySampleDB
SELECT NAME FROM sys.sysdatabases
GO
Screenshot-sql-cmd-create-list-DB
NOTE: you can run any other sql command here.
QUIT
use the escape sequence Ctrl-p + Ctrl-q
docker stop sql2017
To restart the image using previous container name 'sql2017'. It will start the container with previous settings
docker start sql2017
- Article on how to setup docker for sql: https://database.guide/how-to-install-sql-server-on-a-mac/
- Docker's commands information: https://docs.docker.com/v17.09/compose/reference/
- MS SQL Docker setup and commands:
- https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-configure-docker?view=sql-server-2017
- https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-2017
- Azure Studio (cross-platform SQL Studio with limited capabilites): https://docs.microsoft.com/en-us/sql/azure-data-studio/what-is?view=sql-server-2017
Create a minimal website using template called 'webapp' and additionally specifing addon using --auth
dotnet new webapp --auth Individual -o WebApp1
Server=127.0.0.1,1401;Database=MySampleDB;User Id=SA;Password=Strong!Passw0rd
The default sql server for this git repo was SQLite but I wanted to use full SQL server hosted on my Docker image So made follwing changes in 2 files
// FROM
"DefaultConnection": "DataSource=app.db"
// TO
"DefaultConnection": "Server=127.0.0.1,1401;Database=MySiteDB;User Id=SA;Password=MyStrongPassword"
// FROM
UseSqlite
// TO
UseSqlServer
// FROM
SQLite:Autoincrement
// TO
Autoincrement
dotnet ef database update
dotnet build
dotnet run
It should show you where website is hosted locally on your machine. You should be able to go to that localhost (ignore https warning and proceed).
if you want to add more functionality For example : a class/table i.e. Project and its scaffolded views etc.
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet aspnet-codegenerator identity -dc WebApp1.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout"
You might get 'No executable found matching command "dotnet-aspnet-codegenerator"' error when you ran last command . It means you might be missing dotnetcore code generation tool on your machine. Before netcore 2.1 release it used to be a local nuget package reference but with 2.1 release it has to be installed gloabblly. To fix that run following command and it will install dotnet code generation tool on your machine globally.
dotnet tool install --global dotnet-aspnet-codegenerator
Screenshot-dotnet-apnet-generator
Now let's Create a new model class i.e. Project.cs in Models folder. See example in refrenced MS Doc (dotnet model)
Create one more class i.e. ProjectDBContext.cs in 'Data' folder. See example in refrenced MS Doc (dotnet model)
Now we have the Model class we can scaffold controller actions and CRUD views under Views folder. To do that we will use 'aspnet-codegenerator'.
dotnet aspnet-codegenerator controller -name ProjectsController -m Project -dc ProjectDBContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
Make sure you have it installed already (above 2.1 you need to install it globallly on your local machine before 2.1 dotnet core it can be installed locally to a project)
Now in StartUp.cs -> ConfigureServices method add following line after the existing AddDbContext command
services.AddDbContext<ProjectDBContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
If you already have more then 1 context then you need to pass --context . the one you added in ConfigureServices
dotnet ef migrations add InitialProjectModelCreate --context ProjectDBContext
Above command basically generates a script to create the initial database schema. The database schema is based on the model specified in the ProjectDBContext class (in the Data/ProjectDBContext.cs file).
dotnet ef database update --context ProjectDBContext
dotnet build
dotnet run
Simply add properties with Data Annotations in your Model/Project.cs class as mentioned in refrenced link (MS Doc below). Then run the following commands:
dotnet ef migrations add InitialProjectDBModelUpdate --context ProjectDBContext
dotnet ef database update --context ProjectDBContext
- dotnet core authroization : https://docs.microsoft.com/en-us/aspnet/core/security/authorization/simple?view=aspnetcore-2.2
- dotnet model: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.2&tabs=visual-studio-code
- How to add a new model: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.2&tabs=visual-studio
- How to add new field to your existing model: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/new-field?view=aspnetcore-2.2&tabs=visual-studio