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にこの愚かなことをどうやってつなぐことができますか?
の子要素である??? – Alex
私のproject.jsonでは、EntityFramework.Coreが既に追加されていますか?私は私の参照リストにそれを見ることができます。それは実際に追加されています。 – Samir
あなたはエンティティフレームワークの中核をconfigファイルのリストの一番上に近づけることができますか?順序は重要かもしれませんし、EF自体を宣言する前にあなたのコマンドとSQLサーバーがあります。 – Spluf