npgsql和Entity Framework代码首先设置问题 [英] npgsql and Entity Framework code first setup problems

查看:190
本文介绍了npgsql和Entity Framework代码首先设置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近的错误是

ERROR: 42P01: relation "dbo.__MigrationHistory" does not exist

但我相信这只是因为早期的东西没有正确设置。

but im convinced that this is just because something earlier hasnt been set up properly.

我目前试图设置实体框架4.4代码,首先使用Npgsql 2.0.12,我已经做了以下工作,似乎至少连接到数据库现在,但是当我做了上面的错误context.saveChanges();

Im currently trying to set up entity framework 4.4 code first to use Npgsql 2.0.12, I have done the following and it seems to atleast be connecting to the database now but giving me the above error when I do context.saveChanges();


  • 更新了.net 2.0.50727的machine.config;

  • Updated the machine.config for .net 2.0.50727 with;

< add name =Npgsql Data Providerinvariant =Npgsqlsupport =FF
description =。Postgresql Server的Ne​​t Framework数据提供者
type =Npgsql.NpgsqlFactory,Npgsql,Version = 2.0 .12.0,Culture = neutral,
PublicKeyToken = 5d8b90d52f46fda7/>

< add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />

将dll添加到项目

将app.config更改为这样;

Changed the app.config to look like this;

<configuration>
  <configSections>
    <section name="entityFramework"
      type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
      EntityFramework, 
      Version=4.4.0.0, Culture=neutral, 
      PublicKeyToken=b77a5c561934e089" 
      requirePermission="false" />
  </configSections>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql"></remove>
      <add name="Npgsql Data Provider" 
           invariant="Npgsql" 
           description=".Net Framework Data Provider for Postgresql Server" 
           type="Npgsql.NpgsqlFactory, Npgsql, 
                 Version=2.0.12.0, Culture=neutral, 
                 PublicKeyToken=5d8b90d52f46fda7" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="DataContext" 
         connectionString="Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=*******;CommandTimeout=20;" 
         providerName="Npgsql" />
  </connectionStrings>
</configuration>


  • 数据传递类似于以下

  • Data passing in looks like the following

    public class Animal
    {
    [Key]
    public int Id {get;组; }
    public string Name {get;组; }
    public string描述{get;组; }
    public int Age {get;组; }
    public int NoOfLegs {get;组;
    }

    public class Animal { [Key] public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public int Age { get; set; } public int NoOfLegs { get; set; } }

    其他一切都是通用的设置上下文

    Everything else is generic off the shelf set up of contexts

    任何有助于我做错什么或提示或教程,什么都有帮助。这只是一个概念的一点证据,但我不会介意让它工作。

    Any help on what I'm doing wrong or tip or tutorials, anything would be helpful. This was just a little proof of concept but I wouldnt mind getting it working.

    P.s。对于错误使用代码格式,抱歉,堆叠交换不会让我正确使用它,因为某种原因,即使格式正确。

    P.s. sorry for the bad use of code formatting, stack exchange will not let me use it properly for some reason even though its formatted correctly.

    推荐答案

    Npgsql不支持模式创建,因此您必须手动创建数据库。
    然后为了避免这个错误,将这个语句添加到代码的某个地方(在你的情况下,它可能在Main()函数的开头):

    Npgsql doesn't support schema creation, so you have to create db manually. Then to avoid this error add this statement somewhere in your code (in your case it might be on the beginning of Main() function):

    Database.SetInitializer<DataContext>(null);
    

    代替DataContext使用您的DbContext实现。

    Instead of DataContext use your DbContext implementation.

    这篇关于npgsql和Entity Framework代码首先设置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆