Asp.net core and Entityframwork migration error

.net asp.net asp.net-core c# entity-framework-core

Question

I have models folder and in folder i have Todo.cs and TodoContext.cs

In Todo.cs my code is:

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

namespace ApiCrudWithEfCore.Models
{
    public class Todo
    {
        public int Id { get; set; }
        public string title { get; set; }
        public bool Iscomplete { get; set; }

    }
}

And in TodoContext i have :

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

namespace ApiCrudWithEfCore.Models
{
    public class TodoContext : DbContext
    {
        public TodoContext(DbContextOptions<TodoContext> options) :base(options) {}

        public DbSet<Todo> Todos { get; set; }

    }
}

After that i put my connection string in appsettings.json :

{ connection: "Server=(localdb)\mssqllocaldb;Database=Todo;Trusted_Connection=True;", "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*" }

And i use it in startup.cs like this:

 public void ConfigureServices(IServiceCollection services,IConfiguration config)
        {

            services.AddMvc();

            services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
        }

But when i use Add-Migration command it give me this error:

An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: The ConfigureServices method must either be parameterless or take only one parameter of type IServiceCollection. Unable to create an object of type 'TodoContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I read that but can't find helpfull for me can anyone help me?

1
-1
6/26/2019 9:36:10 AM

Accepted Answer

I found my problem first of all i changed my appsettings.json file as suggested and it look like this:

{
  "ConnectionStrings": { "todo": "Server=(localdb)\\mssqllocaldb;Database=Todo;Trusted_Connection=True;" },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

But it didn't fix the problem so i start checking startup.cs and change the way i inject appsettings so my startup.cs file look like this:

        public IConfiguration _config { get; set; }
    public Startup(IConfiguration configuration) {
        _config = configuration;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc();

        services.AddDbContext<TodoContext>(options => options.UseSqlServer(_config.GetSection("ConnectionStrings")["todo"]));
    }

I inject Iconfiguration in startup constructor and define _config var and assign configuration to it after that i use GetSection to get connection string

-1
6/28/2019 6:54:00 AM

Popular Answer

You can try registering db context in this way as well:

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<TodoContext>(options => options.UseSqlServer(Configuration.GetConnectionString("connection")));
    ...
}

or you can manually register the db context:

services.AddTransient<TodoContext>();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow