Question

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.

1
0
10/1/2018 11:00:35 AM

Accepted Answer

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

0
10/1/2018 11:09:41 AM

Popular Answer

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());
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow