gpt4 book ai didi

c# - 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext ]' violates the constraint of type parameter ' TContext'

转载 作者:太空狗 更新时间:2023-10-29 20:38:43 24 4
gpt4 key购买 nike

这是我第一次使用 asp 5\core1,我在设置 Entity Framework 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; }

}

关于 Entity Framework ,我的 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 的表,但没有为我的类(class)创建任何表,所以我使用了 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'.

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

谢谢

最佳答案

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

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

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

关于c# - 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext ]' violates the constraint of type parameter ' TContext',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37461729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com