Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Edited on

     

Quick notes on efcore for postgres

Got to be quick :)

Add the postgres ef library to your project fromNuget.org

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

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

```csharpservices.AddDbContext<DdlContext>(options =>            options.UseNpgsql(Configuration.GetConnectionString("MoviesConnection"))        );```
Enter fullscreen modeExit fullscreen mode

Now Add Your ContextClassMoivesContext

```csharppublic class DdlContext : DbContext{    public DbSet<Movie> Movies { get; set; }    public DdlContext(DbContextOptions<DdlContext> options) : base(options)    {    }       }```
Enter fullscreen modeExit fullscreen mode

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

```csharpprotected 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 modeExit fullscreen mode

Originally posted onBitsmonkey
Photo by Nam Anh on Unsplash

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromArjun Shetty

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp