5

它採用asp 5 \核1我的第一次,我有問題設置一個實體框架的DbContext「Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]」違反了類型參數的約束「TContext」

我有一個類庫和我對象

public class Utilizador 
    { 
     public Utilizador() 
     { 

     } 

     public int id { get; set; } 
    } 

然後,我有一個參考我的課和上下文

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

    } 

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

} 

我的package.json其LIK一個Web API項目E本關於實體框架

"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"])); 

    } 

我用命令添加的遷移,並創建了數據庫和一個表叫__MigrationsHistory但沒有任何創建我的類,所以我使用的外接遷移「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'. 

可有人請點我在正確的方向?我做錯了什麼,我在這裏錯過了什麼?

感謝

回答

0

嘗試增加數據庫初始化對上下文:

編輯:我最初的答案是基於實體框架6 Database.EnsureCreated()聽起來像它可能是在最新的對應方法發佈:

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

didnt工作甚至不能找到數據庫的方法,我知道有在最後一個版本,但一些相當大的變化用它它很難找到PPL ...:/ – Shakawkaw

+0

問題竟是包依賴a nd工具,我redid它,並添加了一些更多的l,重新啓動vs現在它的工作...也許是一個錯誤的參考或者是什麼問題...如果需要將稍後發佈更改...謝謝 – Shakawkaw

+1

@Shakawkaw你能發佈究竟是什麼修復你的問題?我有同一個。在學習教程之後,我不確定我的project.json工具和依賴關係究竟出了什麼問題。 – Hank

相關問題