DEV Community

Cover image for Quick notes on efcore for postgres
Arjun Shetty
Arjun Shetty

Posted on • Edited on

3

Quick notes on efcore for postgres

Got to be quick :)

Add the postgres ef library to your project from Nuget.org

`dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 3.1.3`
Enter fullscreen mode Exit fullscreen mode

Setting the efcore middleware on Startup.cs, and picking the connection string from app settings

```csharp
services.AddDbContext<DdlContext>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("MoviesConnection"))
        );
```
Enter fullscreen mode Exit fullscreen mode

Now Add Your ContextClass MoivesContext

```csharp
public class DdlContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }

    public DdlContext(DbContextOptions<DdlContext> options) : base(options)
    {

    }       
}
```
Enter fullscreen mode Exit fullscreen mode

In case you have an existingDB which has table and column names different form your Models you can override modelling

```csharp
protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Movie>(x =>
            {
                x.ToTable("movies_new");


                x.Property(y => y.Title).HasColumnName("movie_name");
                x.Property(y => y.Year).HasColumnName("movie_year");
                x.Property(y => y.Rating).HasColumnName("movie_rating");
            }
        );

    }
```
Enter fullscreen mode Exit fullscreen mode

Originally posted on Bitsmonkey
Photo by Nam Anh on Unsplash

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more