2016-07-15 12 views
0

私は今、コンテキストクラスを自分のアプリケーションに成功させずに注入するのにかなり苦労してきました。エンティティのフレームワーク/ DbContextの設定

私が作成するdbcontextクラスはgeneric型でなければならないのはなぜですか?または私は何か間違っているのですか?ありがとうございました。

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、Version = v1.0

エラーは単純ですが、わかりません。あなたの答えをありがとうございます。ここにはコード付き

+0

コードをテキストではなく画像として追加してください – stuartd

+0

Microsoft.Data.Entityの代わりにSystem.Data.Entityを使用してください。 – Alegrowin

答えて

0

、私はName spaces)以下 1をチェックしてくださいと言うことができます - あなたのコンテキストクラスは、クラスライブラリにある場合は、プロジェクトを参照(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

関連する問題