Sunday, March 13, 2022

Add New DataBase with sample Table Entity Core .Net 6/WebApi

 

appsetting.json 

 

 


"ConnectionStrings": {
    "DefaultConnection": "Server=TestPC\\SQLEXPRESS;Database=BaseProductAPI;User Id=sa;Password=123;Trusted_Connection=True;"

  }

 

 

 

 Program.cs

 


builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));

});

 

Model

Product.cs

 

 

 

 namespace Base.Services.ProductAPI.Models
{
    public class Product
    {
        [Key]
        public int ProductId { get; set; }
        [Required]
        public string? Name { get; set; }
        [Range(1, 1000)]
        public double Price { get; set; }
        public string? Description { get; set; }
        public string? CategoryName { get; set; }
        public string? ImageUrl { get; set; }
    }
}

Nuget Console 


PM> add-migration TestTableToDB

PM> update-database