首先为代码配置 EF 和 Npgsql
Configuring EF and Npgsql for code first
我正在尝试让 EF 与 Postgresql 一起工作。如果可能的话,我想要代码优先迁移。到目前为止,我得到了一个简单的 ADO 命令,所以我很确定至少我的连接字符串是正确的。但是我无法让 EF 代码迁移在我的生活中发挥作用,而且我找不到任何最近提供帮助的指南。
我用NuGet引入了Npgsql,EntityFramework和EntityFramework6.Npgsql。我在 pgadmin 中创建了一个名为 blogsample 的空数据库。
目前,当我 运行 应用程序时,它在第 29 行抛出,当我尝试将博客添加到数据库时:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
这是我的 .cs 文件:
using Npgsql;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace NpgSqlTest
{
class Program
{
static void Main(string[] args)
{
EFTest();
}
private static void EFTest()
{
using (var db = new BloggingEntities())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
private static void ADOTest()
{
string ConnectionString = ConfigurationManager.ConnectionStrings["PostgresDotNet"].ConnectionString;
using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
const string sql = "SELECT * from sample;";
NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
DataSet ds = new DataSet();
using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd))
{
adapter.Fill(ds);
}
int i = 0;
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class BloggingEntities : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// PostgreSQL uses the public schema by default - not dbo.
modelBuilder.HasDefaultSchema("public");
base.OnModelCreating(modelBuilder);
}
}
}
还有我的App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.data>
<DbProviderFactories>
<!--for EF4.x and EF6.0.x -->
<!--you may need this. if you don't modify machine.config-->
<remove invariant="Npgsql" />
<add name="Npgsql - .Net Data Provider for PostgreSQL" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=3.0.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<!--for EF6.0.x -->
<!--you need this. add it manually-->
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<connectionStrings>
<add name="PostgresDotNet"
connectionString="User ID=sava;Password=abc;Host=localhost;Port=5432;Database=blogsample;Pooling=true;"
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
您的上下文似乎没有使用您的连接字符串。使用上下文中的构造函数执行此操作:
public class BloggingEntities : DbContext
{
public BloggingEntities()
: base("PostgresDotNet") { }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// PostgreSQL uses the public schema by default - not dbo.
modelBuilder.HasDefaultSchema("public");
base.OnModelCreating(modelBuilder);
}
}
http://fdevel.blogspot.com/2013/12/npgsql-with-entity-framework-6.html
我正在尝试让 EF 与 Postgresql 一起工作。如果可能的话,我想要代码优先迁移。到目前为止,我得到了一个简单的 ADO 命令,所以我很确定至少我的连接字符串是正确的。但是我无法让 EF 代码迁移在我的生活中发挥作用,而且我找不到任何最近提供帮助的指南。
我用NuGet引入了Npgsql,EntityFramework和EntityFramework6.Npgsql。我在 pgadmin 中创建了一个名为 blogsample 的空数据库。
目前,当我 运行 应用程序时,它在第 29 行抛出,当我尝试将博客添加到数据库时:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
这是我的 .cs 文件:
using Npgsql;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace NpgSqlTest
{
class Program
{
static void Main(string[] args)
{
EFTest();
}
private static void EFTest()
{
using (var db = new BloggingEntities())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
private static void ADOTest()
{
string ConnectionString = ConfigurationManager.ConnectionStrings["PostgresDotNet"].ConnectionString;
using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
const string sql = "SELECT * from sample;";
NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
DataSet ds = new DataSet();
using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd))
{
adapter.Fill(ds);
}
int i = 0;
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
public class BloggingEntities : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// PostgreSQL uses the public schema by default - not dbo.
modelBuilder.HasDefaultSchema("public");
base.OnModelCreating(modelBuilder);
}
}
}
还有我的App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.data>
<DbProviderFactories>
<!--for EF4.x and EF6.0.x -->
<!--you may need this. if you don't modify machine.config-->
<remove invariant="Npgsql" />
<add name="Npgsql - .Net Data Provider for PostgreSQL" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=3.0.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<!--for EF6.0.x -->
<!--you need this. add it manually-->
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<connectionStrings>
<add name="PostgresDotNet"
connectionString="User ID=sava;Password=abc;Host=localhost;Port=5432;Database=blogsample;Pooling=true;"
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
您的上下文似乎没有使用您的连接字符串。使用上下文中的构造函数执行此操作:
public class BloggingEntities : DbContext
{
public BloggingEntities()
: base("PostgresDotNet") { }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// PostgreSQL uses the public schema by default - not dbo.
modelBuilder.HasDefaultSchema("public");
base.OnModelCreating(modelBuilder);
}
}
http://fdevel.blogspot.com/2013/12/npgsql-with-entity-framework-6.html