
Got to be quick :)
Add the postgres ef library to your project fromNuget.org
`dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 3.1.3`
Setting the efcore middleware onStartup.cs
, and picking the connection string from app settings
```csharpservices.AddDbContext<DdlContext>(options => options.UseNpgsql(Configuration.GetConnectionString("MoviesConnection")) );```
Now Add Your ContextClassMoivesContext
```csharppublic 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
```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"); } ); }```
Originally posted onBitsmonkey
Photo by Nam Anh on Unsplash
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse