【问题标题】:'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext''Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext]' 违反类型参数 'TContext' 的约束
【发布时间】:2016-10-15 15:27:22
【问题描述】:

这是我第一次使用 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'.

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

谢谢

【问题讨论】:

    标签: c# entity-framework dbcontext entity-framework-core asp.net-core-1.0


    【解决方案1】:

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

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

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

    【讨论】:

    • 没有工作,甚至找不到数据库的那些方法,我知道上一个版本有一些相当大的变化,而且很难找到使用它的人......:/
    • 问题实际上出在包依赖项和工具中,我重做了它并添加了更多 l,重新启动 vs 现在它可以工作了......也许是一个错误的参考或问题......如果需要将在稍后发布更改...谢谢
    • @Shakawkaw 你能准确地发布解决你问题的方法吗?我也有同样的。按照教程学习,所以我不确定我的 project.json 工具和依赖项到底出了什么问题。
    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 2018-10-04
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多