@manhng

Welcome to my blog!

EF Core PostgreSQL Generate Script Update Database

March 17, 2021 17:15

EF Core PostgreSQL Generate Script Update Database (edit)

https://stackoverflow.com/questions/42355481/auto-create-database-in-entity-framework-core/

http://www.binaryintellect.net/articles/87446533-54b3-41ad-bea9-994091686a55.aspx

https://www.craftedpod.com/tech/ef-core-basic-and-custom-migrations-for-postgresql/

[ASP.NET Core][Entity Framework Core][Npgsql] Try Code First & DB First

https://dev.to/masanori_msl/asp-net-core-entity-framework-core-npgsql-try-code-first-db-first-32pj

The DatabaseGenerated Attribute

https://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/databasegenerated-attribute

Fluent API
The Fluent API equivalent mehtods for the DatabaseGenerated attribute are

Computed: ValueGeneratedOnAddOrUpdate
Identity: ValueGeneratedOnAdd
None: ValueGeneratedNever

Implement many-to-many relationships without mapping join table

https://dev.to/_patrickgod/many-to-many-relationship-with-entity-framework-core-4059

How to Configure PostgreSQL in Entity Framework Core:

https://code-maze.com/configure-postgresql-ef-core/

How to configure EF Core to generate UUID:

https://garywoodfine.com/how-to-generate-postgresql-uuid-with-ef-core/

EF Core Features:

https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/

For the curious, who want to take a look and examine the SQL Scripts that will be generated to create the migrations you can use
dotnet ef migrations script

Common queries in PostgreSQL

http://dotnet-concept.com/Articles/2020/7/5800890/Common-queries-in-PostgreSQL-Database-Create-Alter-Drop

Unit Test + EF Core in-memory

https://dev.to/vbilopav/net-identity-with-custom-postgresql-store-migrations-unit-tests-and-norm-net-57p8

EF Core + PostgreSQL + Npgsql

PostgreSQL Database Connection String:
Host=localhost;port=5432;Database=test;Username=postgres;Password=Test@123

C:\>
md DotNetCore
cd DotNetCore
dotnet new empty -n CodeFirstSample
cd CodeFirstSample

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design

notepad appsettings.Development.json
notepad Startup.cs

md Models
cd Models
notepad Workflow.cs
notepad WorkflowReader.cs
notepad CodeFirstSampleContext.cs

cd C:\DotNetCore\CodeFirstSample>

dotnet tool update dotnet-ef --global

dotnet build

CodeFirstSample -> C:\DotNetCore\CodeFirstSample\bin\Debug\net5.0\CodeFirstSample.dll

dotnet ef migrations add InitialCreate

dotnet ef database update

dotnet ef migrations script

cd C:\DotNetCore\
C:\DotNetCore\>

dotnet new empty -n DbFirstSample

cd C:\DotNetCore\DbFirstSample

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design

dotnet ef dbcontext scaffold "Host=localhost;port=5432;Database=test;Username=postgres;Password=Test@123" Npgsql.EntityFrameworkCore.PostgreSQL -d -o Models -n Models

Assembly Npgsql.EntityFrameworkCore.PostgreSQL, Version=5.0.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7
Assembly: Npgsql.EntityFrameworkCore.PostgreSQL.dll
Namespace: Npgsql.EntityFrameworkCore.PostgreSQL.Metadata
Enum: NpgsqlValueGenerationStrategy

public enum NpgsqlValueGenerationStrategy
{
None = 0,
SequenceHiLo = 1,
SerialColumn = 2,
IdentityAlwaysColumn = 3,
IdentityByDefaultColumn = 4
}

Categories

Recent posts