Last active
September 29, 2025 14:22
-
-
Save ljamel/bdacd3993f3bd61b2107205392886761 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
// =================================================== | |
// Auteur : Lamri : Alias ingenius | |
// Projet : "Déploiement d'application dotnet CRUD avec sql server " | |
// Date : 2025-09-29 | |
// =================================================== | |
sudo apt-get update && | |
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18 mssql-tools unixodbc-dev && | |
# Ajouter sqlcmd au PATH | |
export PATH="$PATH:/opt/mssql-tools/bin" | |
# --- Créer le projet MVC --- | |
dotnet new mvc -n CrudDemo && | |
cd CrudDemo | |
# --- Supprimer conteneur SQL Server existant si nécessaire --- | |
docker rm -f sqlserver 2>/dev/null || true | |
# --- Lancer SQL Server Docker --- | |
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=StrongPass123!' \ | |
-p 1433:1433 --name sqlserver -d mcr.microsoft.com/mssql/server:2022-latest | |
sleep 15 | |
# --- Créer la base de données --- | |
sqlcmd -S 127.0.0.1,1433 -U sa -P StrongPass123! -Q "IF DB_ID('NomDeTaBase') IS NULL CREATE DATABASE NomDeTaBase;" | |
# --- Installer outils scaffolding .NET --- | |
dotnet tool install -g dotnet-aspnet-codegenerator | |
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design | |
dotnet add package Microsoft.EntityFrameworkCore.SqlServer | |
dotnet add package Microsoft.EntityFrameworkCore.Tools | |
dotnet tool install -g dotnet-ef | |
# Ajouter dotnet tools au PATH | |
export PATH="$PATH:$HOME/.dotnet/tools" | |
# --- Créer le modèle Produit --- | |
dotnet new class -n Produit -o Models | |
echo "using System.ComponentModel.DataAnnotations; | |
namespace CrudDemo.Models | |
{ | |
public class Produit | |
{ | |
[Key] | |
public int Id { get; set; } | |
[Required] | |
public string Nom { get; set; } | |
public decimal Prix { get; set; } | |
} | |
}" > Models/Produit.cs | |
dotnet aspnet-codegenerator controller \ | |
-name ProduitsController \ | |
-m Produit \ | |
-dc ApplicationDbContext \ | |
--relativeFolderPath Controllers \ | |
--useDefaultLayout \ | |
--referenceScriptLibraries | |
sed -i 's/context\.Produit/context.Produits/g' Controllers/ProduitsController.cs | |
echo "using Microsoft.EntityFrameworkCore; | |
using CrudDemo.Models; | |
namespace CrudDemo; | |
public class ApplicationDbContext : DbContext | |
{ | |
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) | |
: base(options) { } | |
public DbSet<Produit> Produits { get; set; } | |
}" > Data/ApplicationDbContext.cs | |
echo "using CrudDemo; | |
using Microsoft.EntityFrameworkCore; | |
var builder = WebApplication.CreateBuilder(args); | |
// Ajouter le DbContext SQL Server Docker | |
builder.Services.AddDbContext<ApplicationDbContext>(options => | |
options.UseSqlServer(builder.Configuration.GetConnectionString(\"DefaultConnection\"))); | |
builder.Services.AddControllersWithViews(); | |
var app = builder.Build(); | |
if (!app.Environment.IsDevelopment()) | |
{ | |
app.UseExceptionHandler(\"/Home/Error\"); | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
app.UseRouting(); | |
app.UseAuthorization(); | |
app.MapControllerRoute( | |
name: \"default\", | |
pattern: \"{controller=Home}/{action=Index}/{id?}\"); | |
app.Run();" > Program.cs | |
sed -i 's#Server=(localdb).*#Server=127.0.0.1,1433;Database=NomDeTaBase;User Id=sa;Password=StrongPass123!;Encrypt=False;TrustServerCertificate=True;"#' appsettings.json | |
sed -i 's/"ApplicationDbContext"/"DefaultConnection"/' appsettings.json | |
# --- Scaffold du DbContext à partir de SQL Server --- | |
dotnet ef dbcontext scaffold "Server=127.0.0.1,1433;Database=NomDeTaBase;User Id=sa;Password=StrongPass123!;Encrypt=False;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models | |
# --- Scaffold du CRUD complet --- | |
dotnet aspnet-codegenerator controller -name ProduitsController -m Produit -dc ApplicationDbContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries | |
dotnet ef migrations add InitialCreate --context ApplicationDbContext | |
dotnet ef database update --context ApplicationDbContext | |
sed -i '/asp-controller="Home" asp-action="Privacy"/a \ <li class="nav-item"><a class="nav-link text-dark" asp-area="" asp-controller="Produits" asp-action="Index">Produits</a></li>' "$(pwd)/Views/Shared/_Layout.cshtml" | |
echo "CRUD MVC avec SQL Server créé avec succès !" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment