Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to adjust NpgsqlDataSource #3

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
]
},
"dotnet-outdated-tool": {
"version": "3.2.1",
"version": "4.6.0",
"commands": [
"dotnet-outdated"
]
},
"dotnet-ef": {
"version": "8.0.0",
"version": "8.0.8",
"commands": [
"dotnet-ef"
]
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore
Expand All @@ -26,7 +26,7 @@ jobs:
run: dotnet build --no-restore

- name: run IntegreSQL docker
run: cd scripts; docker-compose up -d
run: cd scripts; docker compose up -d

- name: Test
run: dotnet test --no-build --verbosity normal --logger "trx;LogFileName=test-results.trx"
Expand Down
14 changes: 14 additions & 0 deletions IntegreSql.EF.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleWeb.IntegrationTests
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleWeb.UnitTests", "tests\ExampleWeb.UnitTests\ExampleWeb.UnitTests.csproj", "{AB078860-06D1-4611-BBBA-1D7E96C9B421}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleWebPostgresSpecific", "examples\ExampleWebPostgresSpecific\ExampleWebPostgresSpecific.csproj", "{3AD86800-36E2-48B9-A3A0-428B6C0EEBFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleWebPostgresSpecific.UnitTests", "tests\ExampleWebPostgresSpecific.UnitTests\ExampleWebPostgresSpecific.UnitTests.csproj", "{8538E23A-C48D-4229-91CE-3767198CB583}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -34,10 +38,20 @@ Global
{AB078860-06D1-4611-BBBA-1D7E96C9B421}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB078860-06D1-4611-BBBA-1D7E96C9B421}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB078860-06D1-4611-BBBA-1D7E96C9B421}.Release|Any CPU.Build.0 = Release|Any CPU
{3AD86800-36E2-48B9-A3A0-428B6C0EEBFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AD86800-36E2-48B9-A3A0-428B6C0EEBFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AD86800-36E2-48B9-A3A0-428B6C0EEBFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AD86800-36E2-48B9-A3A0-428B6C0EEBFE}.Release|Any CPU.Build.0 = Release|Any CPU
{8538E23A-C48D-4229-91CE-3767198CB583}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8538E23A-C48D-4229-91CE-3767198CB583}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8538E23A-C48D-4229-91CE-3767198CB583}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8538E23A-C48D-4229-91CE-3767198CB583}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B8D87F8F-F622-48A1-BEEA-4C8BA6B1F6E9} = {0F0A5C71-7CAD-46EB-9A89-21CDDA43815D}
{802044F5-BD36-4ECD-8ABD-96E742EE6F36} = {6A62837E-674A-444E-AEF4-177681A1D816}
{AB078860-06D1-4611-BBBA-1D7E96C9B421} = {6A62837E-674A-444E-AEF4-177681A1D816}
{3AD86800-36E2-48B9-A3A0-428B6C0EEBFE} = {0F0A5C71-7CAD-46EB-9A89-21CDDA43815D}
{8538E23A-C48D-4229-91CE-3767198CB583} = {6A62837E-674A-444E-AEF4-177681A1D816}
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion examples/ExampleWeb/Database/ExampleDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public class ExampleDbContext : DbContext
{
public DbSet<User> Users { get; set; }

public ExampleDbContext(DbContextOptions<ExampleDbContext> options) : base(options) { }
public ExampleDbContext(DbContextOptions<ExampleDbContext> options)
: base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/ExampleWeb/Database/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public class User
{
public int Id { get; set; }
public string Name { get; set; }

Check warning on line 6 in examples/ExampleWeb/Database/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
}
13 changes: 8 additions & 5 deletions examples/ExampleWeb/ExampleWeb.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.8" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// <auto-generated />
using System.Collections.Generic;
using ExampleWeb;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -16,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.5")
.HasAnnotation("ProductVersion", "8.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
Expand All @@ -35,7 +36,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("Id");

b.ToTable("Users");
b.ToTable("Users", (string)null);

b.HasData(
new
Expand Down
17 changes: 8 additions & 9 deletions examples/ExampleWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using ExampleWeb;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<UserService>();
builder.Services.AddDbContext<ExampleDbContext>(
options => options.UseNpgsql(builder.Configuration.GetValue<string>("Postgres"))
builder.Services.AddDbContext<ExampleDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetValue<string>("Postgres"))
);

// builder.Services.AddDbContext<ExampleDbContext>(
Expand All @@ -21,8 +20,8 @@
// if you use CreateDatabaseGetConnectionString).
if (app.Configuration.GetValue<bool>("DisableSeed") != true)
{
await app.Services
.CreateScope()
await app
.Services.CreateScope()
.ServiceProvider.GetRequiredService<ExampleDbContext>()
.Database.MigrateAsync();
}
Expand All @@ -32,10 +31,10 @@
"/database-type",
(ExampleDbContext dbContext) =>
dbContext.Database.IsNpgsql()
? "postgres"
: dbContext.Database.IsSqlite()
? "sqlite"
: "unknown"
? "postgres"
: dbContext.Database.IsSqlite()
? "sqlite"
: "unknown"
);
app.MapGet(
"/users",
Expand All @@ -48,7 +47,7 @@
{
var user = await context.Request.ReadFromJsonAsync<User>();
var dbContext = context.RequestServices.GetRequiredService<ExampleDbContext>();
dbContext.Users.Add(user);

Check warning on line 50 in examples/ExampleWeb/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'entity' in 'EntityEntry<User> DbSet<User>.Add(User entity)'.
await dbContext.SaveChangesAsync();
}
);
Expand Down
12 changes: 12 additions & 0 deletions examples/ExampleWebPostgresSpecific/Database/Document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace ExampleWebPostgresSpecific.Database;

public class Document
{
public int Id { get; set; }
public string Name { get; set; }

Check warning on line 8 in examples/ExampleWebPostgresSpecific/Database/Document.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[Column(TypeName = "jsonb")]
public List<Document>? SubDocuments { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;

namespace ExampleWebPostgresSpecific.Database;

public class ExamplePostgresSpecificDbContext : DbContext
{
public DbSet<User> Users { get; set; }

public ExamplePostgresSpecificDbContext(
DbContextOptions<ExamplePostgresSpecificDbContext> options
)
: base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>(e =>
{
e.Property(x => x.Documents).HasColumnType("jsonb");
e.HasData(new() { Id = 1, Name = "John" }, new() { Id = 2, Name = "Bill" });
});
}
}
8 changes: 8 additions & 0 deletions examples/ExampleWebPostgresSpecific/Database/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ExampleWebPostgresSpecific.Database;

public class User
{
public int Id { get; set; }
public string Name { get; set; }

Check warning on line 6 in examples/ExampleWebPostgresSpecific/Database/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public List<Document>? Documents { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.8" />
<PackageReference Include="Npgsql.Json.NET" Version="8.0.4" />
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using ExampleWebPostgresSpecific.Database;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace ExampleWebPostgresSpecific.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table
.Column<int>(type: "integer", nullable: false)
.Annotation(
"Npgsql:ValueGenerationStrategy",
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn
),
Name = table.Column<string>(type: "text", nullable: false),
Documents = table.Column<List<Document>>(type: "jsonb", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
}
);

migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Documents", "Name" },
values: new object[,]
{
{ 1, null, "John" },
{ 2, null, "Bill" }
}
);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: "Users");
}
}
}
Loading
Loading