2016-07-15 9 views
0

Ich habe schon seit geraumer Zeit gekämpft, um meine Kontextklasse ohne Erfolg in meine Anwendung zu injizieren.Entitätsframework/DbContext konfigurieren

Warum muss die Klasse dbcontext, die ich erstelle, vom generischen Typ sein? Oder mache ich etwas falsch? Vielen Dank.

Startup.cs:

using Microsoft.AspNetCore.Builder; 
    using Microsoft.AspNetCore.Hosting; 
    using Microsoft.Extensions.Configuration; 
    using Microsoft.Extensions.DependencyInjection; 
    using Microsoft.Extensions.Logging; 
    using Car_proj_new.Models; 
    using Microsoft.Data.Entity; 


    namespace Car_proj_new 
    { 
     public class Startup 
     { 
      public Startup(IHostingEnvironment env) 
      { 
       var builder = new ConfigurationBuilder() 
        .SetBasePath(env.ContentRootPath) 
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
        .AddEnvironmentVariables(); 
       Configuration = builder.Build(); 
      } 

      public IConfigurationRoot Configuration { get; } 


      // This method gets called by the runtime. Use this method to add services to the container. 
      public void ConfigureServices(IServiceCollection services) 
      { 
       // Add framework services. 

       var connection = Configuration["Data:ConnectionString"]; 

       services.AddEntityFramework().AddDbContext<CarDbContext>(options => { options.UseSqlServer(connection); }); 




       services.AddMvc(); 
      } 
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
      { 
       loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
       loggerFactory.AddDebug(); 

       if (env.IsDevelopment()) 
       { 
        app.UseDeveloperExceptionPage(); 
        app.UseBrowserLink(); 
       } 
       else 
       { 
        app.UseExceptionHandler("/Home/Error"); 
       } 

       app.UseStaticFiles(); 

       app.UseMvc(routes => 
       { 
        routes.MapRoute(
         name: "default", 
         template: "{controller=Home}/{action=Index}/{id?}"); 
       }); 
      } 
     } 
    } 

Db Kontextklasse:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.EntityFrameworkCore; 

namespace Car_proj_new.Models 
{ 
    public class CarDbContext: DbContext 
    { 

     public DbSet<Corporations> Corporations { get; set; } 
     public DbSet<PossibleFixes> PossibleFixes { get; set; } 
     public DbSet<Workers> Workers { get; set; } 

    } 
} 

Project.Json:

{ 
    "dependencies": { 
    "EntityFramework.Core": "7.0.0-rc1-final", 
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", 
    "Microsoft.AspNetCore.Diagnostics": "1.0.0", 
    "Microsoft.AspNetCore.Mvc": "1.0.0", 
    "Microsoft.AspNetCore.Razor.Tools": { 
     "version": "1.0.0-preview2-final", 
     "type": "build" 
    }, 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", 
    "Microsoft.AspNetCore.StaticFiles": "1.0.0", 
    "Microsoft.EntityFrameworkCore": "1.0.0", 
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", 
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0", 
    "Microsoft.Extensions.Logging": "1.0.0", 
    "Microsoft.Extensions.Logging.Console": "1.0.0", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0", 
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", 
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0" 
    }, 

    "tools": { 
    "BundlerMinifier.Core": "2.0.238", 
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final", 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" 
    }, 

    "frameworks": { 
    "netcoreapp1.0": { 
     "imports": [ 
     "dotnet5.6", 
     "dnxcore50", 
     "portable-net45+win8" 
     ] 
    } 
    }, 

    "buildOptions": { 
    "emitEntryPoint": true, 
    "preserveCompilationContext": true 
    }, 

    "publishOptions": { 
    "include": [ 
     "wwwroot", 
     "Views", 
     "Areas/**/Views", 
     "appsettings.json", 
     "web.config" 
    ] 
    }, 

    "scripts": { 
    "prepublish": [ "bower install", "dotnet bundle" ], 
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] 
    } 
} 

Der Fehler:

Fehler CS0311 Der Typ ‚Car_proj_new .Modelle.CarD bContext 'kann nicht als Typparameter' TContext 'im generischen Typ oder in der Methode' EntityFrameworkServicesBuilder.AddDbContext (Action) 'verwendet werden. Es gibt keine implizite Referenzkonvertierung von "Car_proj_new.Models.CarDbContext" in "Microsoft.Data.Entity.DbContext". Car_proj_new.NETCoreApp, Version = v1.0

Der Fehler scheint einfach, aber ich kann es nicht herausfinden, ich schätze Ihre Antworten.

+0

Bitte fügen Sie Ihren Code als Text nicht Bilder – stuartd

+0

Vielleicht sollten Sie System.Data.Entity anstelle von Microsoft.Data.Entity verwenden – Alegrowin

Antwort

0

ohne Code hier kann ich sagen, bitte die folgenden 1) Name spaces überprüfen - Wenn Ihre Kontext-Klasse in einer Klassenbibliothek ist, prüfen, ob das Projekt als Referenz hinzugefügt wird (project.json) 2) Sieht aus wie Sie einen Verweis auf EF Kern haben - mein * erraten * aber die Klasse erzeugt Kontext von oder verwendet full .net framework

ist hier eine Probe

using Microsoft.EntityFrameworkCore; 

services.AddDbContext<MyContext>(options => { options.UseSqlServer(connection); }); 
{ 
    "dependencies": { 
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", 
    "Microsoft.EntityFrameworkCore": "1.0.0", 
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", 
    }, 
    "frameworks": { 
    "netcoreapp1.0": { 
     "imports": [ 
     "dotnet5.6", 
     "dnxcore50", 
     "portable-net45+win8" 
     ] 
    } 
    } 
} 

Das obige funktioniert für mich. tech stack .net core und EF Core