5

Es ist mein erstes Mal asp 5 \ core1 verwenden und ich habe Problem DbContext'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1 [TContext]' verletzt die Einschränkung des Typs Parameter 'TContext'

ein Entity Framework Einstellung

ich habe eine Klassenbibliothek mit meinem Objekt

public class Utilizador 
    { 
     public Utilizador() 
     { 

     } 

     public int id { get; set; } 
    } 

Dann habe ich ein Web-Api-Projekt mit einem Verweise auf meine Klasse und einen Kontext

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

    } 

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

} 

Mein package.json seiner lik e in Bezug auf diese 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" 
     ] 
    } 
    } 

Und schließlich mein startup.cs

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

    } 

verwendete ich den Befehl Add-Migrationen und es erstellt meine Datenbank und eine Tabelle namens __MigrationsHistory aber hatten keine für meine Klasse erstellt so i verwendet, um die Add-Migration „mycontext“ und alles aufgehört zu arbeiten, jetzt jedes Mal wenn ich versuche, eine Migration zu tun bekomme ich diesen Fehler:

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

Kann jemand bitte zeigen ich in die richtige Richtung? Was mache ich falsch, was fehlt mir hier?

dank

Antwort

0

versuchen, eine Datenbank initializer zu Ihrem Kontext hinzu:

Edit: Meine erste Antwort beruhte auf Entity Framework 6. Database.EnsureCreated() klingt wie es die entsprechende Methode in der neuesten sein könnte Mitteilung:

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

nur knapp sein Ziel kann nicht funktionierte auch diese Methoden für Datenbank finden, ich weiß, dass einige ziemlich große Veränderungen in der letzten Version und es ist schwierig, ppl zu finden mit ihm gab es noch ...:/ – Shakawkaw

+0

das Problem war eigentlich in die Paketabhängigkeiten a nd tools, ich habe es neu gemacht und etwas mehr l hinzugefügt, neu gestartet vs und jetzt funktioniert es ... vielleicht war eine schlechte referenz oder etwas das problem ... wenn nötig werden die änderungen später ... danke – Shakawkaw

+1

@Shakawkaw Kannst du posten Genau was hat dein Problem behoben? Ich habe das gleiche. Nach einem Tutorial zu lernen, bin ich mir nicht sicher, was genau ist falsch mit meinen Project.json Tools und Abhängigkeiten. – Hank

Verwandte Themen