Skip to content

Commit 84ecb43

Browse files
austindrenskiroji
authored andcommitted
Updated docs for 2.1, modernized the examples
1 parent d256b7f commit 84ecb43

1 file changed

Lines changed: 50 additions & 28 deletions

File tree

doc/index.md

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,78 @@
11
# Getting Started
22

3-
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.
3+
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.
44

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

7-
## Using the Npgsql EF Core Provider
7+
## Configuring the project file
88

9-
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/).
9+
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/).
1010

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

1313
```xml
1414
<Project Sdk="Microsoft.NET.Sdk">
1515
<PropertyGroup>
16-
<OutputType>Exe</OutputType>
17-
<TargetFramework>netcoreapp2.0</TargetFramework>
16+
<TargetFramework>netcoreapp2.1</TargetFramework>
1817
</PropertyGroup>
1918
<ItemGroup>
20-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />
21-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
22-
</ItemGroup>
23-
<ItemGroup>
24-
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
19+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.0" />
2520
</ItemGroup>
2621
</Project>
2722
```
2823

29-
## Using the Npgsql provider
24+
## Defining a `DbContext`
3025

31-
public class BlogContext : DbContext
26+
```c#
27+
using System.Collections.Generic;
28+
using Microsoft.EntityFrameworkCore;
29+
30+
namespace ConsoleApp.PostgreSQL
31+
{
32+
public class BloggingContext : DbContext
3233
{
33-
// When used with ASP.net core, add these lines to Startup.cs
34-
// var connectionString = Configuration.GetConnectionString("BlogContext");
35-
// services.AddEntityFrameworkNpgsql().AddDbContext<BlogContext>(options => options.UseNpgsql(connectionString));
36-
// and add this to appSettings.json
37-
// "ConnectionStrings": { "BlogContext": "Server=localhost;Database=blog" }
34+
public DbSet<Blog> Blogs { get; set; }
3835

39-
public BlogContext(DbContextOptions<BlogContext> options) : base(options) { }
36+
public DbSet<Post> Posts { get; set; }
4037

41-
public DbSet<BlogPost> BlogPosts { get; set; }
38+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
39+
=> optionsBuilder.UseNpgsql("Host=my_host;Database=my_db;Username=my_user;Password=my_pw");
4240
}
43-
44-
## Using an Existing Database (Database-First)
4541

46-
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:
42+
public class Blog
43+
{
44+
public int BlogId { get; set; }
45+
public string Url { get; set; }
4746

48-
```bash
49-
dotnet ef dbcontext scaffold "Host=localhost;Database=mydatabase;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL
47+
public List<Post> Posts { get; set; }
48+
}
49+
50+
public class Post
51+
{
52+
public int PostId { get; set; }
53+
public string Title { get; set; }
54+
public string Content { get; set; }
55+
56+
public int BlogId { get; set; }
57+
public Blog Blog { get; set; }
58+
}
59+
}
5060
```
5161

52-
Or with Powershell:
62+
## Additional configuration for ASP.NET Core applications
63+
64+
Modify the `ConfigureServices` method in `Startup.cs`:
5365

54-
```powershell
55-
Scaffold-DbContext "Host=localhost;Database=mydatabase;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL
66+
```c#
67+
public IServiceProvider ConfigureServices(IServiceCollection services)
68+
=> services.AddEntityFrameworkNpgsql()
69+
.AddDbContext<BlogContext>()
70+
.BuildServiceProvider();
5671
```
72+
## Using an Existing Database (Database-First)
73+
74+
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:
75+
76+
```bash
77+
dotnet ef dbcontext scaffold "Host=my_host;Database=my_db;Username=my_user;Password=my_pw" Npgsql.EntityFrameworkCore.PostgreSQL
78+
```

0 commit comments

Comments
 (0)