Reputation: 41
I'm pretty new to C#. I'm getting some red squiggles(error underlines) on .UseNpgsql(string connectionString)
below. The hovering error tells me that I'm probably not using the right using statement.
using Microsoft.Data.Entity;
namespace Something.Context {
public class OauthTokenContext : DbContext
{
public DbSet<Model.OauthToken> OauthTokens { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
var connectionString = @"Server=localhost; Port=1337; User Id=tinganho; Password=secret; Database=hello";
builder.UseNpgsql(connectionString);
}
}
}
In my project.json
I have the following dependencies:
"dependencies": {
"Npgsql.EntityFramework7": "3.1.0-unstable0038",
"Npgsql": "3.1.0-unstable0038"
}
And in startup.cs
:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.AspNet.Routing;
using System.Linq;
using Microsoft.Data.Entity;
using Microsoft.Framework.DependencyInjection;
using Npgsql;
using Npgsql.EntityFramework7;
using Something.Context;
using Something.Model;
namespace Something
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
}
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddDbContext<OauthTokenContext>();
services.AddMvc();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
}
public IConfiguration Configuration { get; set; }
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
// Add MVC to the request pipeline.
app.UseMvc();
// Add the following route for porting Web API 2 controllers.
// routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
}
}
}
And when I add .AddNpgsql()
after services.AddEntityFramework()
I get:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Framework.DependencyInjection.Abstractions' or one of its dependencies
Upvotes: 2
Views: 2092
Reputation: 990
You should use this approach:
1) into Startup.ConfigureServices add only services.AddEntityFrameworkNpgsql();
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddEntityFrameworkNpgsql();
}
2) into class inherit the DbContext:
public class DatabaseClass : DbContext
{
//...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("my connection string");
}
}
Upvotes: 2