'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]'违反了类型参数'TContext'的约束 [英] 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext'

查看:85
本文介绍了'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]'违反了类型参数'TContext'的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用asp 5\core1,在设置实体框架dbcontext时遇到问题

It's my first time using asp 5\core1 and i'm having problem setting a entity framework dbcontext

我的对象有一个类库

public class Utilizador
    {
        public Utilizador()
        {

        }

        public int id { get; set; }
    }

然后我有一个Web Api项目,该项目引用了我的课程和context

Then i have a Web Api project with a reference to my class and a context

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

    }

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

}

我的package.json与实体框架类似,

My package.json its like this regarding entity framework

"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

And finally my 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,并且一切都停止了,现在每次我尝试进行迁移时,都会出现此错误:

I used the command Add-Migrations and it created my database and one table called __MigrationsHistory but didnt created any for my class so i used the Add-Migration "mycontext" and everything stopped working, now everytime i try to do a migration i get this error:

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'.

有人可以指出我正确的方向吗?我在做什么错,我在这里想念什么?

Can someone please point me in the right direction? what am i doing wrong, what am i missing here?

谢谢

推荐答案

尝试将数据库初始化程序添加到您的上下文中:

Try adding a database initializer to your context:

编辑:我的最初答案是基于Entity Framework6。Database.EnsureCreated()听起来像是最新版本中的等效方法:

My initial answer was based on Entity Framework 6. Database.EnsureCreated() sounds like it might be the equivalent method in the latest release:

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

这篇关于'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]'违反了类型参数'TContext'的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆