无法连接到EntityFramework

时间:2016-04-18 07:39:39

标签: c# .net entity-framework

startup.cs

Dreampress

project.json依赖项

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);
    }
}

如果我使用EntityFramework.Core v7.0.0.0-rc1-final我收到以下错误:

我在这行上的options.UseSqlServer下面有一个红线:

  "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"
  },

错误说:

  

类型&#39; EntityOptionsBuilder&#39;在一个不是的程序集中定义   引用。您必须添加对程序集的引用   &#39; EntityFramework.Core,Version = 7.0.0.0,Culture = neutral,   公钥=空&#39;

如果我使用EntityFramework.Core v7.0.0-beta5,我会收到以下错误:

此行上AddEntityFramework下的红线:

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

错误如下:

  

&#39; IServiceCollection&#39;不包含的定义   &#39; AddEntityFramework&#39;并且没有扩展方法&#39; AddEntityFramework&#39;   接受类型&#39; IServiceCollection&#39;的第一个参数。可以找到   (您是否缺少using指令或程序集引用?)

当我在线查看时,虽然看起来我找到的用于设置连接的所有教程都涉及一条类似于我的产生该错误的行。也许这是一个简单的问题,而且我缺乏c#的经验让我不知道明显的答案?如何将这个愚蠢的东西连接到我的SQL DB?

2 个答案:

答案 0 :(得分: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依赖项运行不同的版本,这可能也是一个问题...很高兴我可以提供帮助:)

答案 1 :(得分:0)

确保您的连接字符串正确无误。确保正确安装了相关的nuget包。正确安装nuget软件包并使用您引用的正确名称连接字符串正确后,例如<add name="DbName" connectionString="xyn" providerName="System.Data.SqlClient" /> 确保将db上下文称为DbName或连接字符串中的名称。确保使用正确的using System.Data.SqlClient;

确保您拥有EF的coirrect配置实体,例如

  <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>

这是<configuration>

的子元素