mustafa arslantaş
mustafa arslantaş

Reputation: 81

I cannot create migrations in ASP.NET Core. What is the solution?

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();
            });
        }
    }
}

Upvotes: 1

Views: 4958

Answers (3)

TRK
TRK

Reputation: 198

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.

Upvotes: 1

mustafa arslantaş
mustafa arslantaş

Reputation: 81

I removed Entity Framework 6 and got a solution thanks

Upvotes: 0

J Weezy
J Weezy

Reputation: 3945

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

Upvotes: 1

Related Questions