|
1 | 1 | # Getting Started |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | Development happens in the [Npgsql.EntityFrameworkCore.PostgreSQL](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL) repository, all issues should be reported there. |
6 | 6 |
|
7 | | -## Using the Npgsql EF Core Provider |
| 7 | +## Configuring the project file |
8 | 8 |
|
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/). |
10 | 10 |
|
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: |
12 | 12 |
|
13 | 13 | ```xml |
14 | 14 | <Project Sdk="Microsoft.NET.Sdk"> |
15 | 15 | <PropertyGroup> |
16 | | - <OutputType>Exe</OutputType> |
17 | | - <TargetFramework>netcoreapp2.0</TargetFramework> |
| 16 | + <TargetFramework>netcoreapp2.1</TargetFramework> |
18 | 17 | </PropertyGroup> |
19 | 18 | <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" /> |
25 | 20 | </ItemGroup> |
26 | 21 | </Project> |
27 | 22 | ``` |
28 | 23 |
|
29 | | -## Using the Npgsql provider |
| 24 | +## Defining a `DbContext` |
30 | 25 |
|
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 |
32 | 33 | { |
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; } |
38 | 35 |
|
39 | | - public BlogContext(DbContextOptions<BlogContext> options) : base(options) { } |
| 36 | + public DbSet<Post> Posts { get; set; } |
40 | 37 |
|
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"); |
42 | 40 | } |
43 | | - |
44 | | -## Using an Existing Database (Database-First) |
45 | 41 |
|
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; } |
47 | 46 |
|
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 | +} |
50 | 60 | ``` |
51 | 61 |
|
52 | | -Or with Powershell: |
| 62 | +## Additional configuration for ASP.NET Core applications |
| 63 | + |
| 64 | +Modify the `ConfigureServices` method in `Startup.cs`: |
53 | 65 |
|
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(); |
56 | 71 | ``` |
| 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