Skip to content

Commit

Permalink
Updated docs for 2.1, modernized the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
austindrenski authored and roji committed Jun 19, 2018
1 parent d256b7f commit 84ecb43
Showing 1 changed file with 50 additions and 28 deletions.
78 changes: 50 additions & 28 deletions doc/index.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@
# Getting Started

Npgsql has an Entity Framework Core provider. It mostly behaves like a any other EFCore provider (e.g. SQL Server) - all the information in the [general EF Core docs](https://docs.microsoft.com/en-us/ef/core/index) applies. If you're just getting started with EF Core, those docs are the best place to start.
Npgsql has an Entity Framework (EF) Core provider. It behaves like other EF Core providers (e.g. SQL Server), so the [general EF Core docs](https://docs.microsoft.com/en-us/ef/core/index) apply here as well. If you're just getting started with EF Core, those docs are the best place to start.

Development happens in the [Npgsql.EntityFrameworkCore.PostgreSQL](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL) repository, all issues should be reported there.

## Using the Npgsql EF Core Provider
## Configuring the project file

To use the Npgsql EF Core provider, simply add a dependency on `Npgsql.EntityFrameworkCore.PostgreSQL`. You can follow the instructions in the general [EF Core Getting Started docs](https://docs.microsoft.com/en-us/ef/core/get-started/).
To use the Npgsql EF Core provider, add a dependency on `Npgsql.EntityFrameworkCore.PostgreSQL`. You can follow the instructions in the general [EF Core Getting Started docs](https://docs.microsoft.com/en-us/ef/core/get-started/).

Following is an example (new-style) csproj using Npgsql EF Core:
Below is a `.csproj` file for a console application that uses the Npgsql EF Core provider:

```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.0" />
</ItemGroup>
</Project>
```

## Using the Npgsql provider
## Defining a `DbContext`

public class BlogContext : DbContext
```c#
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApp.PostgreSQL
{
public class BloggingContext : DbContext
{
// When used with ASP.net core, add these lines to Startup.cs
// var connectionString = Configuration.GetConnectionString("BlogContext");
// services.AddEntityFrameworkNpgsql().AddDbContext<BlogContext>(options => options.UseNpgsql(connectionString));
// and add this to appSettings.json
// "ConnectionStrings": { "BlogContext": "Server=localhost;Database=blog" }
public DbSet<Blog> Blogs { get; set; }

public BlogContext(DbContextOptions<BlogContext> options) : base(options) { }
public DbSet<Post> Posts { get; set; }

public DbSet<BlogPost> BlogPosts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql("Host=my_host;Database=my_db;Username=my_user;Password=my_pw");
}

## Using an Existing Database (Database-First)

The Npgsql EF Core provider also supports reverse-engineering a code model from an existing PostgreSQL database ("database-first"). To do so, execute the following if you're using dotnet cli:
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }

```bash
dotnet ef dbcontext scaffold "Host=localhost;Database=mydatabase;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL
public List<Post> Posts { get; set; }
}

public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }

public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}
```

Or with Powershell:
## Additional configuration for ASP.NET Core applications

Modify the `ConfigureServices` method in `Startup.cs`:

```powershell
Scaffold-DbContext "Host=localhost;Database=mydatabase;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL
```c#
public IServiceProvider ConfigureServices(IServiceCollection services)
=> services.AddEntityFrameworkNpgsql()
.AddDbContext<BlogContext>()
.BuildServiceProvider();
```
## Using an Existing Database (Database-First)

The Npgsql EF Core provider also supports reverse-engineering a code model from an existing PostgreSQL database ("database-first"). To do so, use dotnet CLI to execute the following:

```bash
dotnet ef dbcontext scaffold "Host=my_host;Database=my_db;Username=my_user;Password=my_pw" Npgsql.EntityFrameworkCore.PostgreSQL
```

0 comments on commit 84ecb43

Please sign in to comment.