Tyler
Tyler

Reputation: 3813

Can't connect to EntityFramework

startup.cs

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

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

If I use EntityFramework.Core v7.0.0.0-rc1-final I get the following error:

I have a red line under options.UseSqlServer on this line:

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

The error says:

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

If I use EntityFramework.Core v7.0.0-beta5 I get the following error:

A red line under AddEntityFramework on this line:

services.AddEntityFramework()

The error reads:

'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?)

When I look online though it seems all of the tutorials I find for setting up the connection involve a line similar to mine which is producing that error. Perhaps this is a simple issue and my lack of experience in c# keeps me from knowing the obvious answer? How can I get this stupid thing connected to my SQL DB?

Upvotes: 1

Views: 473

Answers (2)

Spluf
Spluf

Reputation: 820

Try removing everything related to EF and just put this:

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

EF should solve other dependencies by itself... also in your package config you have different versions running for some of your EF dependencies, that might also be a problem... glad I could help :)

Upvotes: 3

user2336660
user2336660

Reputation:

Ensure that your connection strings are correct. Ensure that the relevent nuget packages are installed correctly. Once your nuget packages are installed correctly and your connection string is correct with a proper name that youre referencing e.g. <add name="DbName" connectionString="xyn" providerName="System.Data.SqlClient" /> Ensure you refer to your db context as DbName or whatever is the name in the connection string. Ensure you use the correct using using System.Data.SqlClient;

Ensure you ahve the coirrect config entity for EF e.g.

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

This is a child element of <configuration>

Upvotes: 0

Related Questions