question

JerryLipan-2008 avatar image
0 Votes"
JerryLipan-2008 asked DuaneArnold-0443 answered

Need help EntityFrameworkCore DbContext

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


dotnet-aspnet-core-mvc
001.png (15.4 KiB)
002.png (17.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It's one line of code in the startup.cs file. First, I think you'll be interested in reading the official documentation which explains everything you need to know.


0 Votes 0 ·
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JerryLipan-2008 avatar image
0 Votes"
JerryLipan-2008 answered AgaveJoe commented

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





01.png (16.3 KiB)
02.png (38.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Seems the connection string is missing. Try using the debugger to troubleshoot your code. Also, can you explain the design? Is the intent to create a separate DbContext for stored procedures???

0 Votes 0 ·