2016-04-18 12 views
1

startup.csはEntityFramework

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Hosting; 
using Microsoft.AspNet.Http; 
using Microsoft.Extensions.DependencyInjection; 

using MyQuotesApp.models; 

using Microsoft.Framework.Runtime; 
using Microsoft.Framework.Configuration; 
using Microsoft.Data.Entity; 

namespace MyQuotesApp 
{ 
    public class Startup 
    { 
     public IConfiguration Configuration { get; set; } 

     public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) 
     { 
      var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath); 
      configurationBuilder.AddJsonFile("config.json"); 
      configurationBuilder.AddEnvironmentVariables(); 
      Configuration = configurationBuilder.Build(); 
     } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 
     public void ConfigureServices(IServiceCollection services) 
     { 
      services.AddMvc(); 

      services.AddEntityFramework() 
       .AddSqlServer() 
       .AddDbContext<QuotesAppContext>(options => options.UseSqlServer(Configuration["Data:ConnectionString"])); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app) 
     { 
      app.UseMvc(); 
      app.UseDefaultFiles(); 
      app.UseStaticFiles(); 
     } 

     // Entry point for the application. 
     public static void Main(string[] args) => WebApplication.Run<Startup>(args); 
    } 
} 

project.jsonの依存関係

"dependencies": { 
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 
    "EntityFramework.SqlServer": "7.0.0-beta5", 
    "EntityFramework.Commands": "7.0.0-beta5", 
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta5", 
    "Microsoft.Framework.Configuration.EnvironmentVariables": "1.0.0-beta5", 
    "EntityFramework.Core": "7.0.0-rc1-final", 
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final" 
    }, 

を接続できません次のエラーが表示されます。

options.UseSqlServの下に赤い線がありますえー、この行に:

.AddDbContext<QuotesAppContext>(options => options.UseSqlServer(Configuration["Data:ConnectionString"])); 

エラーは言う:

The type 'EntityOptionsBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null'.

私はEntityFramework.CoreのV7.0.0-beta5を使用している場合は、私は次のエラーを取得する:

赤い線の下でこの行のAddEntityFramework:

services.AddEntityFramework() 

エラーが表示されます:

'IServiceCollection' does not contain a definition for 'AddEntityFramework' and no extension method 'AddEntityFramework' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

私は接続を設定するためのチュートリアルのすべてに、そのエラーを生成している鉱山に似た行が含まれているようですが、私はオンラインで見ています。おそらく、これは簡単な問題であり、C#での私の経験の欠如は私が明白な答えを知るのを妨げているでしょうか?私のSQL DBにこの愚かなことをどうやってつなぐことができますか?

+0

の子要素である??? – Alex

+0

私のproject.jsonでは、EntityFramework.Coreが既に追加されていますか?私は私の参照リストにそれを見ることができます。それは実際に追加されています。 – Samir

+1

あなたはエンティティフレームワークの中核をconfigファイルのリストの一番上に近づけることができますか?順序は重要かもしれませんし、EF自体を宣言する前にあなたのコマンドとSQLサーバーがあります。 – Spluf

答えて

3

EFに関連するすべてのものを削除してみて、ちょうどこれを置く:

"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", 
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final", 
"EntityFramework.Commands": "7.0.0-rc1-final", 

EFが...それ自体で他の依存関係を解決する必要があり、あなたのパッケージの設定で、あなたのEFの依存関係のいくつかのために実行されている異なるバージョンを持っている、ということ問題になるかもしれません...私は助けてうれしいです:)

0

接続文字列が正しいことを確認してください。関連するナゲットパッケージが正しく取り付けられていることを確認してください。あなたのナゲットパッケージが正しくインストールされ、あなたの接続文字列が正しい名前(例えば、 <add name="DbName" connectionString="xyn" providerName="System.Data.SqlClient" /> DbNameまたは接続文字列内の名前が何であるかをdbコンテキストとして参照してください。 using System.Data.SqlClient;

EFのコイル設定エンティティを確実に使用してください。

<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 

これはまあ、それはEntityFramework.Coreを追加するように言っていない<configuration>