gpt4 book ai didi

c# - Npgsql.Entity Framework 7 无法使用 npgsql 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:13 27 4
gpt4 key购买 nike

我是 C# 的新手。我在下面的 .UseNpgsql(string connectionString) 上看到了一些红色波浪线(错误下划线)。悬停错误告诉我,我可能没有使用正确的 using 语句。

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

在我的 project.json 中,我有以下依赖项:

"dependencies": {
"Npgsql.EntityFramework7": "3.1.0-unstable0038",
"Npgsql": "3.1.0-unstable0038"
}

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

当我在 services.AddEntityFramework() 之后添加 .AddNpgsql() 时,我得到:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Framework.DependencyInjection.Abstractions' or one of its dependencies

最佳答案

你应该使用这种方法:

1) 进入Startup.ConfigureServices只添加services.AddEntityFrameworkNpgsql();

 public void ConfigureServices(IServiceCollection services)
{
//...
services.AddEntityFrameworkNpgsql();
}

2) 入类继承DbContext:

public class DatabaseClass : DbContext 
{
//...

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("my connection string");
}
}

关于c# - Npgsql.Entity Framework 7 无法使用 npgsql 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com