I've manually create DbContext. Here the code
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"CompanyDatabase": "Server=XXXX;Database=XXXX;user id=sa;password=XXXX;"
}
}
getEmployee_Result Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ThirdApp.Models
{
public partial class getEmployee_Result
{
public int Id { get; set; }
public string Name { get; set; }
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public string Designation { get; set; }
}
}
DbContext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace ThirdApp.Models
{
public partial class spCompanyContext : DbContext
{
public DbSet<getEmployee_Result> getEmployee_Result { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Server=XXXX;Database=XXXX;user id=sa;password=XXXX;");
}
}
}
}
Controllers
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThirdApp.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace ThirdApp.Controllers
{
public class EmployeesInGridController : Controller
{
public IActionResult Index()
{
var context = new spCompanyContext();
var getEmployeeList = context.getEmployee_Result
.FromSqlRaw("[dbo].[getEmployee]").ToList();
ViewBag.datasource = getEmployeeList;
return View();
}
}
}
I want to replace ConnectionString in spCompanyContext.cs
How to call CompanyDatabase in appsettings.json ?
Then, my Controllers work as usual. If need adjustment, please tell me the changes
Please help