EF7 Beta 4:AddEntityFramework不接受參數(配置)


我將通過這個例子: http//stephenwalther.com/archive/2015/01/17/asp-net-5-and-angularjs-part-4-using-entity-framework-7

我正在努力解決這個代碼部分:

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Data.Entity;
using creaservo.com.Models;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.AspNet.Hosting;

namespace creaservo.com
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Setup configuration sources.
            Configuration = new Configuration()
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
        }

        public IConfiguration Configuration { get; set; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            // Register Entity Framework
            services.AddEntityFramework(Configuration)
                .AddSqlServer()
                .AddDbContext<MoviesAppContext>();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
        }
    }
}

問題在於

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Data.Entity;
using creaservo.com.Models;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.AspNet.Hosting;

namespace creaservo.com
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Setup configuration sources.
            Configuration = new Configuration()
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
        }

        public IConfiguration Configuration { get; set; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            // Register Entity Framework
            services.AddEntityFramework(Configuration)
                .AddSqlServer()
                .AddDbContext<MoviesAppContext>();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
        }
    }
}

我得到一個構建錯誤:

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Data.Entity;
using creaservo.com.Models;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.AspNet.Hosting;

namespace creaservo.com
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Setup configuration sources.
            Configuration = new Configuration()
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
        }

        public IConfiguration Configuration { get; set; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            // Register Entity Framework
            services.AddEntityFramework(Configuration)
                .AddSqlServer()
                .AddDbContext<MoviesAppContext>();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
        }
    }
}

我在很多其他例子中看到了對Configuration的一個參數的使用。

不知道,有什麼不對......

一般承認的答案

看起來您正在使用的教程使用的是舊版本的EF7框架。 EntityFramework 7 beta 4不再接受AddEntityFramework任何參數。看起來beta 5仍然在同一軌道上。

我相信你要找的是這個:

// Register Entity Framework
services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<MoviesAppContext>(options =>
    {
        options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
    });

這簡化了配置文件中所需的結構,因為MoviesAppContext只需要連接字符串,而不需要EntityFrameworkData元素。





許可下: CC-BY-SA
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因