Quick notes on efcore for postgres
Arjun Shetty
Posted on May 22, 2020
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`
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"))
);
```
Now Add Your ContextClass MoivesContext
```csharp
public class DdlContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
public DdlContext(DbContextOptions<DdlContext> options) : base(options)
{
}
}
```
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");
}
);
}
```
Originally posted on Bitsmonkey
Photo by Nam Anh on Unsplash
💖 💪 🙅 🚩
Arjun Shetty
Posted on May 22, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.