1

Visual Studio 2019 Community. I want to create a table to create the migrations and connect with SQL Server 2014. But when I run

add-migrations AddMusicStoreToDb

I get an error:

add-migrations: The term 'add-migrations' 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.

At line: 1 char: 1
+ add-migrations AddMusicStoreToDb
+ ~~~~~~~~~~~~~~~
+ CategoryInfo: ObjectNotFound: (add-migrations: String) [], CommandNotFoundException
+ FullyQualifiedErrorId: CommandNotFoundException


appsettings.json:

{
  "ConnectingStrings": {
    "DefaultConnection": "Server=DESKTOP-NHG0GU1\\SQLEXPRESS;Database=MusicStoreList  ;Trusted_Connection=True;MultipleActiveResultSets=true;"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

MusicStoreListContext.cs:

using Microsoft.EntityFrameworkCore;

namespace MusicStoreRazor.UI.Models
{
    public class MusicStoreListContext:DbContext
    {
        public MusicStoreListContext(DbContextOptions<MusicStoreListContext> options):base(options)
        {

        }
        public DbSet<Music> Musics { get; set; }
    }
}

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using MusicStoreRazor.UI.Models;

namespace MusicStoreRazor.UI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<MusicStoreListContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddRazorPages().AddRazorRuntimeCompilation();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
  • Check if it is a duplicate of this one https://stackoverflow.com/questions/38173404/the-term-add-migration-is-not-recognized – Steve Apr 28 '20 at 16:05

3 Answers3

1

Have you installed these required NuGet packages to your solution to perform database related operations in asp.net core code first?

1. Microsoft.EntityFrameworkCore.SqlServer: Provide classes to connect with SQL Server for CRUD Operation to Entity Framework Core

2. Microsoft.EntityFrameworkCore.Tools: Help to work with database related activity like add migration, script migration, get DB context, update database, etc

If not then install these packages using the package manager console or NuGet package manager.

j08691
  • 204,283
  • 31
  • 260
  • 272
TRK
  • 188
  • 1
  • 7
  • Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools installed does not show a problem with them. – mustafa arslantaş Apr 28 '20 at 19:55
  • Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Enable-Migrations' for Entity Framework 6. Enable-Migrations is obsolete. Use Add-Migration to start using Migrations. – mustafa arslantaş Apr 28 '20 at 20:50
0

You need to prefix the command with dotnet ef

  1. dotnet ef migrations add AddMusicStoreToDb
  2. dotnet ef database update

https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli

J Weezy
  • 3,507
  • 3
  • 32
  • 88
  • I haven't tried Add-Migration InitialCreate dotnet not available, dotnet not available your commands don't work – mustafa arslantaş Apr 28 '20 at 19:59
  • Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Enable-Migrations' for Entity Framework 6. Enable-Migrations is obsolete. Use Add-Migration to start using Migrations. – mustafa arslantaş Apr 28 '20 at 20:50
  • Isn't that for VSCode and not Visual Studio? Not sure which to use for Community Edition, as I have Professional Edition. – NealWalters Jul 30 '20 at 18:20
  • You type that in the command window in Visual Studio. In VS Code, you would type that in the terminal window. – J Weezy Jul 30 '20 at 18:25
0

I removed Entity Framework 6 and got a solution thanks