Skip to main content

Command Palette

Search for a command to run...

Build the DB using Entity Framework (DbContext)

Updated
2 min read
Build the DB  using Entity Framework (DbContext)
G

gua memang serious cakap lu, gua serious 24 jam. baik di jamban ,di meja makan atau bersenggama . serius...dohhh

salah satu benda yang aku suka dengan Entity Framework dalam ASP.NET ialah DbContext. di mana kau boleh code dulu tanpa perlu create table dalam database. aslong kau ikut pre req dari microsoft, you good to go.

kena install nuget

kemudian dalam appsettings.json create connectionString variable. variable ni kita akan guna Program.cs file.

appsettings.json

  "ConnectionStrings": {
    "ConnectionToDatabase": "Host=192.168.0.xxx; Database=db; Username=notrealdbuser; Password=notarealpasswordnoob!"
  }

}

Program.cs

using Microsoft.EntityFrameworkCore;
using great.Web.Data;

namespace great.Web
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllersWithViews();
            //database
            builder.Services.AddDbContext<BlogDbContext>(options =>
            options.UseNpgsql(builder.Configuration.GetConnectionString("ConnectionToDatabase")));

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}

create folder Data dalam projek dan create class, naming convention untuk class ni ada perkataan "DbContext" , ni untuk memudahkan coder lain baca yang ni ialah DbContext. so ni class file aku yg beri nama greatDbContext

using Microsoft.EntityFrameworkCore;
using great.Web.Models.Domain;




namespace great.Web.Data
{
    public class greatDbContext : DbContext
    {
        public greatDbContext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<BlogPost> BlogPosts { get; set; }
        public DbSet<Tag> Tags { get; set; }

        public DbSet<BlogPostLike> BlogPostLike { get; set; }

        public DbSet<BlogPostComment> BlogPostComment { get; set; }

    }
}

so dengan cara ni , kau just run command kat visual studio / vs code untuk DbContext create table kat db secara auto. tak perlu dah nak buat db dalam db console.

command:

dotnet ef migrations add InitialMigration
dotnet ef database update

More from this blog

G

gua serius

18 posts

serius dohhhh!