2016-07-15 130 views
0

我一直在努力相當長一段時間現在注入我的上下文類到我的應用程序沒有任何成功。配置實體框架/ DbContext

爲什麼我創建的dbcontext類必須是泛型類型?或者我做錯了什麼?謝謝。

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的上下文類:

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%" ] 
    } 
} 

錯誤:

錯誤CS0311類型「Car_proj_new .Models.CarD bContext'不能用作泛型類型或方法'EntityFrameworkServicesBuilder.AddDbContext(Action)'中的類型參數'TContext'。沒有從'Car_proj_new.Models.CarDbContext'到'Microsoft.Data.Entity.DbContext'的隱式引用轉換。 Car_proj_new..NETCoreApp,版本= v1.0

錯誤似乎很簡單,但我無法弄清楚,我很感謝你的回答。

+0

請將您的代碼添加爲文本而不是圖片 – stuartd

+0

也許您應該使用System.Data.Entity而不是Microsoft.Data.Entity – Alegrowin

回答

0

由於這裏沒有代碼,我只能說,請檢查下列 1)Name spaces - 如果你的上下文類是一個類庫,檢查項目被添加作爲參考(project.json) 2)看起來像你由具有到EF核心的參考 - 我*猜測*但所生成的上下文類是從或使用full .net framework

下面是一個示例

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" 
     ] 
    } 
    } 
} 

上面的作品適合我。高科技堆棧.net coreEF Core