🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / .Net Core Web Api / runtime migration initial create otomatik sql database table column oluşturma kullanımı

1-) .Net 6 Core Web Api  - runtime migration initial create otomatik sql database table column oluşturma kullanımı

github link : https://github.com/ramazanhaber/NetCoreV2

önceki makale : https://www.roketnot.com/not/1624-net-6-core-web-api-basit-kullanimi

 

Add-Migration "initialDBoto"

update-database

 

Get-Migration -> migrationlari listeler

 

Remove-Migration -force -> son migrationu siler

 

Program.cs ye aşağıdakini ekle

var app = builder.Build();

 

// otomatik migration

using (var scope = app.Services.CreateScope())

{

    var db = scope.ServiceProvider.GetRequiredService<EpayContext>();

    db.Database.Migrate();

}

 

 

 

1.Adım-> Add-Migration initial-create

2.Adım-> yeni column eklersen bunu çalıştır -> Add-Migration alanlareklendi

3.Adım-> Ogrenci.cs

using System.ComponentModel.DataAnnotations;

namespace NetCoreV2.DataAccesLayer

{

    public class Ogrenci

    {

        [Key]

        public int id { get; set; }

        public string ad { get; set; }

        public int yas { get; set; }

        public string? alan { get; set; }

    }

}

 

4.Adım-> Context.cs

using Microsoft.EntityFrameworkCore;

 

namespace NetCoreV2.DataAccesLayer

{

    public class Context : DbContext

    {

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {

            optionsBuilder.UseSqlServer("Data Source=RAMBO3;Initial Catalog=Deneme;User ID=sa;Password=şifre;TrustServerCertificate=True");

        }

        public DbSet<Ogrenci> Ogrenciler { get; set; }

    }

}

 

 

5.Adım-> Program.cs

 

using Microsoft.EntityFrameworkCore;

using NetCoreV2.DataAccesLayer;

 

var builder = WebApplication.CreateBuilder(args);

 

// Add services to the container.

 

builder.Services.AddControllers();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen();

 

builder.Services.AddDbContext<Context>();

var app = builder.Build();

 

using (var scope = app.Services.CreateScope())

{

    var db = scope.ServiceProvider.GetRequiredService<Context>();

    db.Database.Migrate();

}

 

if (app.Environment.IsDevelopment() || app.Environment.IsProduction())

{

    app.UseDeveloperExceptionPage();

    app.UseSwagger();

    app.UseSwaggerUI(c =>{

        c.SwaggerEndpoint("/swagger/v1/swagger.json", "myapi v1");

    }) ;

}

 

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

 

6.Adım-> OgrenciController.cs

using Microsoft.AspNetCore.Mvc;

using NetCoreV2.DataAccesLayer;

namespace NetCoreV2.Controllers

{

    // https://www.roketnot.com/not/1624-net-6-core-web-api-basit-kullanimi

    [Route("api/[controller]")]

    [ApiController]

    public class OgrenciController : ControllerBase

    {

        [HttpGet]

        public IActionResult EmployeeList()

        {

            using var c = new Context();

            var values = c.Ogrenciler.ToList();

            return Ok(values);

        }

        [HttpPost]

        public IActionResult EmployeeAdd(Ogrenci employee)

        {

            using var c = new Context();

            c.Add(employee);

            c.SaveChanges();

            return Ok();

        }

    }

}

 

 

 

 2024 Nisan 08 Pazartesi
 573