ASP.NET Core 6.0 -Data Annotations - Connection String - Database Migration


Saturday, December 21, 2024    |    419 views

Model
Country.cs
----------------------------------

using System.ComponentModel.DataAnnotations;

namespace WebAppProj.Models
{
    public class Country
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public string  Region { get; set; }
        public DateTime CreatedDate { get; set; }

    }
}

Make Data Folder and Create Class File
ApplicationDbContext.cs
-------------------------------------------------

using Microsoft.EntityFrameworkCore;
using WebAppProj.Models;

namespace WebAppProj.Data
{
    public class ApplicationDbContext:DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
        {

        }

        public DbSet<Country> Countries { get; set; }
    }
}

Create Connection String from
appsettings.json
--------------------------------------------
  "ConnectionStrings": { "DefaultConnection": "Server=.; Database=UtsavMDb; Trusted_Connection=True; MultipleActiveResultSets=True" }

Database Migration
Tools - NuGET Package Manager - Package Manager Console

PM> add-migration AddCategoryToDb
PM> update-database
 

 


Post Comments
222055