简体   繁体   English

npgsql和Entity Framework代码首先设置问题

[英]npgsql and Entity Framework code first setup problems

The most recent error im getting is 我得到的最新错误是

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

but im convinced that this is just because something earlier hasnt been set up properly. 但我确信这只是因为早先没有正确设置。

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(); 我目前正在尝试设置实体框架4.4代码首先使用Npgsql 2.0.12,我已经完成了以下操作,它似乎至少现在连接到数据库,但是当我做context.saveChanges()时给出了上述错误;

  • Updated the machine.config for .net 2.0.50727 with; 更新了.net 2.0.50727的machine.config;

    < 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" /> <add name =“Npgsql Data Provider”invariant =“Npgsql”support =“FF”description =“。Postgresql Server的Ne​​t Framework数据提供程序”type =“Npgsql.NpgsqlFactory,Npgsql,Version = 2.0.12.0,Culture = neutral, PublicKeyToken = 5d8b90d52f46fda7“/>

  • Added the dlls to the project 将dll添加到项目中

  • Changed the app.config to look like this; 将app.config更改为如下所示;

     <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 class Animal {[Key] public int Id {get; set; 组; } public string Name { get; public string Name {get; set; 组; } public string Description { get; } public string描述{get; set; 组; } public int Age { get; } public int Age {get; set; 组; } public int NoOfLegs { get; 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. 这只是一个概念证明,但我不介意让它工作。

Ps 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 doesn't support schema creation, so you have to create db manually. Npgsql不支持模式创建,因此您必须手动创建数据库。 Then to avoid this error add this statement somewhere in your code (in your case it might be on the beginning of Main() function): 然后,为了避免此错误,请在代码中的某处添加此语句(在您的情况下,它可能位于Main()函数的开头):

Database.SetInitializer<DataContext>(null);

Instead of DataContext use your DbContext implementation. 而不是DataContext使用您的DbContext实现。

I agree with the previous answer by iwanek. 我同意iwanek之前的回答。 To be a bit more specific, I like to place the statement in the OnModelCreating override method so that it is always called when the context is created. 更具体一点,我喜欢将语句放在OnModelCreating重写方法中,以便在创建上下文时始终调用它。

    public class MyDbContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<MyDbContext>(null);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM