// using api.Data;using Microsoft.EntityFrameworkCore;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();builder.Services.AddDbContext<ApplicationDBContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));});var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){ app.UseSwagger(); app.UseSwaggerUI();}app.UseHttpsRedirection();app.Run();
// using System.Net.Mime;using System;using System.Collections.Generic;using System.Linq;using System.ComponentModel.DataAnnotations.Schema;using System.Threading.Tasks;namespace api.Models{ public class Stock { public int Id { get; set; } public string Symbol { get; set; } = string.Empty; public string CompanyName { get; set; } = string.Empty; [Column(TypeName = "decimal(18,2)")] public Decimal Purchase { get; set; } [Column(TypeName = "decimal(18,2)")] public Decimal LastDiv { get; set; } public string Industry { get; set; } = string.Empty; public long MarketCap { get; set; } public List<Comment> Comment { get; set; } = new List<Comment>(); }}
// namespace api.Models{ public class Comment { public int Id { get; set; } public string Title { get; set; } = string.Empty; public string Content { get; set; } = string.Empty; public DateTime CreatedOn { get; set; } = DateTime.Now; public int? StockId { get; set; } //navigation property public Stock? Stock { get; set; } }}
// using api.Models;using Microsoft.EntityFrameworkCore;namespace api.Data{ public class ApplicationDBContext : DbContext { public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options) { } public DbSet<Stock> Stock { get; set; } public DbSet<Comment> Comments { get; set; } }}