• Entity Framework Code Firest 连接Postgresql数据库


    1、安装Nuget包

      NpgsqEntityFramework6.Npgsql

      

     2、在App.config添加DbContext(数据库上下文)连接字符串

    <?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" />
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
        </startup>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.3.1" newVersion="4.1.3.1" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <entityFramework>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
          <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
        </providers>
        <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
      </entityFramework>
    <system.data>
        <DbProviderFactories>
          <remove invariant="Npgsql" />
          <add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.1.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
        </DbProviderFactories>
      </system.data>
      <connectionStrings>
        <add name="DataBase" connectionString="server=192.168.0.0;port=8080;Database=Test;uid=user;pwd=123456" providerName="Npgsql" />
      </connectionStrings>
    </configuration>

      server:主机IP、port:端口、Database:数据库名、uid:用户名、pwd=密码

     3、新建一个实体类,用来接收数据库对应的表数据

     public class Cabinets
        {
            public int Id { get; set; }
            public string Address { get; set; }
        }

    4、注册数据库上下文

        public class EfDbContext : DbContext
        {
            public EfDbContext(): base("name=DataBase")
            {
            }
            public virtual DbSet<Cabinets> Cabinets { get; set; }
        }

    5、在主入口Main方法中实现

     static void Main(string[] args)
            {
                try
                {
                    EfDbContext myDatabase = new EfDbContext();
    
                    var list = from s in myDatabase.Cabinets
                               select s;
                    foreach (var item in list)
                    {
                        Console.WriteLine(item.Id);
                    }
    Console.ReadKey(); }
    catch (Exception ex) { Console.WriteLine(ex); Console.ReadKey(); } }

      最后运行出现An error occurred while executing the command definition. See the inner exception for details.的问题

      解决办法:在实体类中上面加上[Table("Cabinets", Schema = "public")]

  • 相关阅读:
    HashMap(HashSet)的实现
    C/C++ 安全编码 —— 指针与内存
    AlexNet神经网络结构
    查看,修改动态组成员/通讯组
    刷新已禁用用户邮箱状态
    监控DAG状态
    AD诊断命令
    PowerShell管理Exchange
    TCP连接(Time_Wait、Close_Wait)说明
    IIS配置(安装IIS、.Net、更改IIS Log目录位置)
  • 原文地址:https://www.cnblogs.com/duhaoran/p/12769067.html
Copyright © 2020-2023  润新知