I cannot connect to Database in Entity Framework

Asked

Viewed 44 times

0

Good night,

I am unable to connect to the database to create the Models folder tables in .NET. When I try to make the Migrations returns this error:

"Could not run because the specified command or file was not found. Possible reasons for this include:

You have incorrectly typed an internal dotnet command. You intended to run a . NET program, but dotnet-Ef does not exist. You intended to run a global tool, but could not find an executable with dotnet prefix with that name in the PATH."

The command I used to make Migrations was this: dotnet Ef Migrations add

I have already made the Dbcontext configuration this way:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APIVirtualCard.Data
{
    public class VCContext : DbContext
    {
        public DbSet<Card> Cards { get; set; }
        public DbSet<Email> Emails { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("server=.;database=myDb;trusted_connection=true;");
        }
    }
}````

1 answer

0

Probably the dotnet "Ef" command was not found, either because it was not installed, or because it was not in your PATH.

To install do: dotnet tool install --global dotnet-ef

To place in PATH, so that it is found and can be used from anywhere:

  • On Windows: Go to edit environment variables and add %USERPROFILE%\.dotnet\tools (or the location where dotnet tools are installed on your system) to the variable PATH.
  • On Linux: Add a line in shell configuration export PATH="$PATH:$HOME/.dotnet/tools/"
  • On macos: Add a line in shell configuration set path = ($path $HOME/.dotnet/tools/)

ref.: https://stackoverflow.com/questions/56862089/cannot-find-command-dotnet-ef

After that, dotnet ef migrations add NomeDescritivoDaSuaMigracao should generate your migration and snapshot in the template, leaving everything right for a subsequent dotnet ef database update

Browser other questions tagged

You are not signed in. Login or sign up in order to post.