Ersin Devrim
Ersin Devrim

Reputation: 117

EF Core 2.0 Npgsql : 42P01

Im learning Postgresql with EF Core 2.0

enter image description here

im keep getting this error. I searched on google but but I am not able to find the right answer.

Here is some detail of my code ; Startup.CS;

public void ConfigureServices(IServiceCollection services)
    {
        var connectionString =  @"User ID=postgres;Password=postgres;Host=localhost;Port=5432;Database=Test;";

        services.AddEntityFrameworkNpgsql()
            .AddDbContext<DataContext>(options => options.UseNpgsql(connectionString));


        services.AddMvc();
    }

and Data Context ;

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {
    }

    public DbSet<Questions> Questions { get; set; }

}

enter image description here

and my model ;

public class Questions
{
    public int Id { get; set; }

    public int studentId { get; set; } 

    public string Title { get; set; }

    public int Votes { get; set; }
}

any help would be appreciated.

Upvotes: 0

Views: 1956

Answers (2)

0909EM
0909EM

Reputation: 5027

Stumbled across this question and, although late to the party, I've also experienced case sensitivity with Oracle (using Oracle.EntityFrameworkCore, where I needed uppercase) and postgres (using Npgsql.EntityFrameworkCore, where I needed lowercase) for table/column names.

I'll add my resolution here so that it helps anyone else in future. I resolved by creating an extension method as follows:

public static void LowercaseRelationalTableAndPropertyNames(this ModelBuilder modelBuilder) 
{
    foreach (var entity in modelBuilder.Model.GetEntityTypes())
    {
        entity.Relational().TableName = entity.Relational().TableName.ToLowerInvariant();

        foreach (var property in entity.GetProperties())
        {
            property.Relational().ColumnName = property.Relational().ColumnName.ToLowerInvariant();
        }
    }
}

and using as follows:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.LowercaseRelationalTableAndPropertyNames();
    }

which allows models to be built with mixed case and converted at runtime as follows:

public class Questions
{
    public int Id { get; set; }
    public int StudentId { get; set; }
}

Note, for EFCore 3.0, the extension method changes as follows:

public static void LowercaseRelationalTableAndPropertyNames(this ModelBuilder modelBuilder)
{
    foreach (var entity in modelBuilder.Model.GetEntityTypes())
    {
        entity.SetTableName(entity.GetTableName().ToLowerInvariant());

        foreach (var property in entity.GetProperties())
        {
            property.SetColumnName(property.GetColumnName().ToLowerInvariant());
        }
    }
}

Upvotes: 1

Ersin Devrim
Ersin Devrim

Reputation: 117

Alright fixed. i think postgresql is case sensetive.

when i changed model equal to database problem is solved.

here is my new model.

enter image description here

Upvotes: 0

Related Questions