How to create Database Model manually?


I have an existing PostgreSQL Database and I want to create the Model for a table manually because scaffolding does not work. Why does running db.CDRs.First(); throw an exception?


        using (var db = new Model.CDRContext())
            var cdr = db.CDRs.First(); // Exception has occurred here


public class CDRContext : DbContext
    public DbSet<CallDataRecord> CDRs { get; set; }

    string DB_SERVER = "";
    string USER = "x";
    string PASSWORD = "x";
    string DATABASE = "x";
    int PORT = 3306; 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseNpgsql($"Host={DB_SERVER};Username={USER}; Port={PORT};Password={PASSWORD};Database={DATABASE};");

This is the detailed exception from the debug console window:

Unhandled exception. Npgsql.PostgresException (0x80004005): 42P01: relation "CDRs" does not exist at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<g__ReadMessageLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) at Npgsql.NpgsqlDataReader.NextResult() at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior) at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.InitializeReader(DbContext _, Boolean result) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.First[TSource](IQueryable1 source) at EFGetStarted.Program.Main(String[] args) in c:\Development\EFGetStarted\Program.cs:line 17 Exception data: Severity: ERROR SqlState: 42P01 MessageText: relation "CDRs" does not exist Position: 45 File: parse_relation.c Line: 894 Routine: parserOpenTable

asked on Stack Overflow Feb 3, 2020 by foobarbaz • edited Feb 3, 2020 by foobarbaz

1 Answer


Try changing the name of your DbSet 'CDRs' to match the name of your database table ('CallDataRecord'?) in the context. Example I've seen for PostgreSQL database first or code first are set up in this way and the error message is due to it looking for a non-existent table CDRs.

  public DbSet<CallDataRecord> YourDbTableName{ get; set; }
answered on Stack Overflow Feb 3, 2020 by JMP

User contributions licensed under CC BY-SA 3.0