Need help EntityFrameworkCore DbContext

Jerry Lipan 916 Reputation points
2021-07-04T14:57:37.307+00:00

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 ?
111458-001.png

Then, my Controllers work as usual. If need adjustment, please tell me the changes
111603-002.png

Please help

Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer

1 additional answer

Sort by: Most helpful
  1. Jerry Lipan 916 Reputation points
    2021-07-05T02:58:04.263+00:00

    Hi @AgaveJoe

    I made some arrangement.

    Startup.cs
    111607-01.png

    spCompanyContext.cs

    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 spCompanyContext(DbContextOptions<spCompanyContext> options)  
               : base(options)  
            {  
            }  
      
            public DbSet<getEmployee_Result> getEmployee_Result { get; set; }  
      
            //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
            //{  
            //    if (!optionsBuilder.IsConfigured)  
            //    {  
            //        optionsBuilder.UseSqlServer(@"Server=DESKTOP-5EFGK9O;Database=Company;user id=sa;password=6181;");  
            //    }  
            //}  
              
      
        }  
    }  
    

    EmployeesInGridController

    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(null);            
      
                var getEmployeeList = context.getEmployee_Result  
                    .FromSqlRaw("[dbo].[getEmployee]").ToList();  
      
                ViewBag.datasource = getEmployeeList;  
      
                return View();  
            }  
        }  
    }  
    

    Got this kind of error,
    111694-02.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.