Database connectivity in ASP.net core , .NET Core DB Connection, step by step
Step1st:
Create ASP.NET Core Project
Step2nd:
Manage Nuget Package Manager Console
Microsoft.EntityFrameworkCore 7.0
Microsoft.EntityFrameworkCore.SqlServer 7.0
Microsoft.EntityFrameworkCore.Tools 7.0
Step3rd:
Create Model Class
namespace WelcomeProject.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Create AppDbContext Class also under model
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace WelcomeProject.Models
{
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
private readonly IConfiguration _configuration;
public AppDbContext(DbContextOptions<AppDbContext> options, IConfiguration configuration)
: base(options)
{
_configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=mydb;Trusted_Connection=True;TrustServerCertificate=True;");
}
}
}
Step4th
Write this command
Add-Migration InitialCreate
Update-Database
Step5th:
Create connection string under appSettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=mydb;Trusted_Connection=True;TrustServerCertificate=True;"
},
"AllowedHosts": "*"
}
Step6th:
//Register services here
builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")
));
Step7th:
Register Controller under Program.cs and run this
Step8th:
Now i want to add Review on Product then i will create Review Model where Review will be child and Product will parent
Create Review Model
namespace WelcomeProject.Models
{
public class Review
{
public int Id { get; set; }
public string Content { get; set; }
public int Rating { get; set; }
public int ProductId { get; set; } // Foreign Key
public Product Product { get; set; } // Navigation Property
}
}
Change Proudct Model also
namespace WelcomeProject.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public ICollection<Review> Reviews { get; set; } // Navigation Property
}
}
Change AppDbContext also
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace WelcomeProject.Models
{
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Review> Reviews { get; set; }
private readonly IConfiguration _configuration;
public AppDbContext(DbContextOptions<AppDbContext> options, IConfiguration configuration)
: base(options)
{
_configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=mydb;Trusted_Connection=True;TrustServerCertificate=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Review>()
.HasOne(r => r.Product)
.WithMany(p => p.Reviews)
.HasForeignKey(r => r.ProductId);
}
}
}
Step9th:
Add following Command
Add-Migration AddReviewTable
Update-Database
Step10th:
Create Review Controller
POST Answer of Questions and ASK to Doubt