'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]'違反了類型參數'TContext'的約束

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

這是我第一次使用asp 5 \ core1而我在設置實體框架dbcontext時遇到了問題

我有一個帶有我的對象的類庫

public class Utilizador
    {
        public Utilizador()
        {

        }

        public int id { get; set; }
    }

然後我有一個Web Api項目,引用了我的類和上下文

public class Context : DbContext
{
    public Context(DbContextOptions<Context> options)
        : base(options)
    {

    }

    public DbSet<Utilizador> Utilizadores { get; set; }

}

我的package.json就像實體框架一樣

"dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.WebApiCompatShim": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Tools":  "1.0.0-preview1-final" ,
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
    "version": "1.0.0-preview1-final",
    "imports": [
      "portable-net45+win8+dnxcore50",
      "portable-net45+win8"
    ]
  }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  }

最後我的startup.cs

            public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
      services.AddEntityFramework().AddEntityFrameworkSqlServer().AddDbContext<Context>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));

    }

我使用命令Add-Migrations,它創建了我的數據庫和一個名為__MigrationsHistory的表,但沒有為我的類創建任何,所以我使用Add-Migration“mycontext”,一切都停止工作,現在每次我嘗試進行遷移我得到這個錯誤:

System.ArgumentException: GenericArguments[0], 'WebApiSolution.Migrations.Context', on 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type 'TContext'. ---> System.TypeLoadException: GenericArguments[0], 'WebApiSolution.Migrations.Context', on 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext'.    at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.Instantiate(Type[] inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)
    at Microsoft.EntityFrameworkCore.Design.DbContextOperations.FindContextFactory(Type contextType) 
    at Microsoft.EntityFrameworkCore.Design.DbContextOperations.FindContextTypes() 
   at Microsoft.EntityFrameworkCore.Design.DbContextOperations.FindContextType(String name)
   at Microsoft.EntityFrameworkCore.Design.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0()
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
 GenericArguments[0], 'WebApiSolution.Migrations.Context', on 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type 'TContext'.

有人可以指出我正確的方向嗎?我做錯了什麼,我在這裡失踪了什麼?

謝謝

熱門答案

嘗試將數據庫初始化程序添加到您的上下文:

編輯:我的初始答案基於實體框架6. Database.EnsureCreated()聽起來像它可能是最新版本中的等效方法:

    public Context(DbContextOptions<Context> options)
        : base(options)
    {
         Database.EnsureCreated();
    }


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow