[Common issues] Entity Framework Core: Database-First

[Common issues] Entity Framework Core: Database-First

while you are trying to generate context using Scaffold-DbContext you may get some errors.

we will list these issues and the solution for them:


1.Scaffold-DbContext Build failed

      make sure that your entire solution has build successfully before run Scaffold-DbContext

2.'Scaffold-DbContext'  Could not load assembly. Ensure it is referenced by the startup project

     here you have two options:
               a. set your project as a startup project.
               b. reference the project dll into the startup project.

3.Your startup project doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again

     here you should install Microsoft.EntityFrameworkCore.Design package by run the following command in Tools –> NuGet Package Manager –> Package Manager Console

            Install-Package Microsoft.EntityFrameworkCore.Design

4.Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.


use the following link to solve this issue: EF - The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet

  


Cannot bulk load. Operating system error code 5 (Access is denied.)

Cannot bulk load. Operating system error code 5 (Access is denied.)


If you are trying to insert bulk data from file using SQL Server and you faced the following error:

Cannot bulk load. Operating system error code 5 (Access is denied.)

Please follow the below steps:

  1- Open Windows services: press Start - > type Services - > Click or tap the matching result.
  2- Right click on SQL Server (MSSQLSERVER) service.
  3- Select Properties.
  4- Switch to Log on tab.
  5- Select Local System Account.
  6- Click OK.
  7- Restart SQL Server (MSSQLSERVER) service : right click on SQL Server (MSSQLSERVER) service -> select Restart.



now try again and you'll not get the same error again.



How to Install windows service using Powershell

How to Install windows service using Powershell

How to install windows service using powershell


$currentpath = $PSScriptRoot;

$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security for the Administrator
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security  for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

# Check to see if powershell currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
   {
   # We are running "as Administrator" - so change the title and background color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {
   # We are not running "as Administrator" - so relaunch as administrator
 
   # Create a new process object that starts PowerShell
   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
 
   # Specify the current script path and name as a parameter
   $newProcess.Arguments = $myInvocation.MyCommand.Definition;
 
   # Indicate that the process should be elevated
   $newProcess.Verb = "runas";
 
   # Start the new process
   [System.Diagnostics.Process]::Start($newProcess);
 
   # Exit from the current, unelevated, process
   exit
   }

   $service = $currentpath + ".\ServiceName.exe";
 
 C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe $service


# Run your code that needs to be elevated here
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")




 



EF - The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet

when you try to generate the EF context for SQL database using the "Scaffold-Context" command in EFCore. But you're getting the following error :
Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.



Solution:

Just install the following "Microsoft.EntityFrameworkCore.Tools" from Nuget by using the following command

Install-Package Microsoft.EntityFrameworkCore.Tools


or right click on the project and select "Manage Nuget Packages" and type in search "Microsoft.EntityFrameworkCore.Tools" select Microsoft.EntityFrameworkCore.Tools like the below image and install.



DryIoc Container Exception Unable to select single public constructor

sometime you'll face the following error:

DryIoc.ContainerException: 'Unable to select single public constructor from implementation type API.Controllers


Solution:

you may have two constructors in the same controller, so you need to remove one of them.









Featured Post

Convert Json to Classes

Followers