2016-07-15 180 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