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

Update TinyHelpers.EntityFrameworkCore to .NET 8.0 #165

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/publish_ef.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
workflow_dispatch:

env:
NET_VERSION: '7.x'
NET_VERSION: '8.x'
PROJECT_NAME: src/TinyHelpers.EntityFrameworkCore
PROJECT_FILE: TinyHelpers.EntityFrameworkCore.csproj
TAG_NAME: ef
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ A collection of helper methods and classes for .NET that I use every day. I have

The library is available on [NuGet](https://www.nuget.org/packages/TinyHelpers). Just search for *TinyHelpers* in the **Package Manager GUI** or run the following command in the **.NET CLI**:

dotnet add package TinyHelpers
```shell
dotnet add package TinyHelpers
```

Check out the [documentation](https://github.com/marcominerva/TinyHelpers/tree/master/docs/TinyHelpers) for a list of all the available helpers.

Expand Down
25 changes: 18 additions & 7 deletions samples/TinyHelpers.EntityFrameworkCore.Sample/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using TinyHelpers.EntityFrameworkCore.Extensions;
using TinyHelpers.EntityFrameworkCore.Sample.Entities;

namespace TinyHelpers.EntityFrameworkCore.Sample;
Expand Down Expand Up @@ -27,17 +26,29 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
builder.Property(x => x.Title).HasMaxLength(80).IsRequired();
builder.Property(x => x.Content).IsRequired();

// Date is a DateOnly property (.NET 6 or higher).
builder.Property(x => x.Date).HasDateOnlyConversion();
// Date is a DateOnly property (.NET 6 or 7).
//builder.Property(x => x.Date).HasDateOnlyConversion();

// Time is a TimeOnly property (.NET 6 or higher).
builder.Property(x => x.Time).HasTimeOnlyConversion();
// Time is a TimeOnly property (.NET 6 or 7).
//builder.Property(x => x.Time).HasTimeOnlyConversion();

/* JSON SUPPORT */

// For .NET 6:
// Reviews is a complex type, this Converter will automatically JSON-de/serialize it
// in a string column.
builder.Property(x => x.Reviews).HasJsonConversion();
// builder.Property(x => x.Reviews).HasJsonConversion();

// For .NET 7 or higher:
builder.OwnsMany(x => x.Reviews).ToJson();

/* COLLECTION OF PRIMITIVE TYPES */

// For .NET 6 and 7:
//builder.Property(x => x.Authors).HasArrayConversion();

builder.Property(x => x.Authors).HasArrayConversion();
// For .NET 8
// The support for collection of primitive types is built-in.
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Post

public string Content { get; set; }

public IEnumerable<string> Authors { get; set; }
public IList<string> Authors { get; set; }

public DateOnly? Date { get; set; }

Expand Down
14 changes: 4 additions & 10 deletions samples/TinyHelpers.EntityFrameworkCore.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@

using var dataContext = new DataContext();

var list = new List<Review>
{
new Review { User = "Pippo", Score = 5 },
new Review { User = "Pluto", Score = 4 }
};

var posts = await dataContext.Posts.Where(p => p.Reviews == list).ToListAsync();

var post = new Post
{
Title = "TinyHelpers",
Expand All @@ -20,14 +12,16 @@
Date = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(-1)),
Reviews = new List<Review>
{
new Review { User = "Pippo", Score = 5, Time=TimeOnly.FromDateTime(DateTime.Now) },
new Review { User = "Pluto", Score = 4 }
new() { User = "Pippo", Score = 5, Time = TimeOnly.FromDateTime(DateTime.Now) },
new() { User = "Pluto", Score = 4 }
}
};

dataContext.Posts.Add(post);
await dataContext.SaveChangesAsync();

var posts = await dataContext.Posts.Where(p => p.Authors.Contains("Marco")).ToListAsync();

Console.ReadLine();

//dataContext.Posts.Add(post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/TinyHelpers.AspNetCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ A collection of helper methods and classes for ASP.NET Core that I use every day

The library is available on [NuGet](https://www.nuget.org/packages/TinyHelpers.AspNetCore). Just search for *TinyHelpers.AspNetCore* in the **Package Manager GUI** or run the following command in the **.NET CLI**:

dotnet add package TinyHelpers.AspNetCore
```shell
dotnet add package TinyHelpers.AspNetCore
```

**Contribute**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TinyHelpers.EntityFrameworkCore.Comparers;

#if NET6_0 || NET7_0
public class DateOnlyComparer : ValueComparer<DateOnly>
{
public DateOnlyComparer() : base(
Expand All @@ -10,3 +11,4 @@ public DateOnlyComparer() : base(
{
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TinyHelpers.EntityFrameworkCore.Comparers;

#if NET6_0 || NET7_0
public class TimeOnlyComparer : ValueComparer<TimeOnly>
{
public TimeOnlyComparer() : base(
Expand All @@ -10,3 +11,4 @@ public TimeOnlyComparer() : base(
{
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TinyHelpers.EntityFrameworkCore.Converters;

#if NET6_0 || NET7_0
public class DateOnlyConverter : ValueConverter<DateOnly, DateTime>
{
public DateOnlyConverter() : base(
Expand All @@ -10,3 +11,4 @@ public DateOnlyConverter() : base(
{
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TinyHelpers.EntityFrameworkCore.Converters;

#if NET6_0 || NET7_0
public class TimeOnlyConverter : ValueConverter<TimeOnly, TimeSpan>
{
public TimeOnlyConverter() : base(
Expand All @@ -10,3 +11,4 @@ public TimeOnlyConverter() : base(
{
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ namespace TinyHelpers.EntityFrameworkCore.Extensions;

public static class PropertyBuilderExtensions
{
#if NET6_0 || NET7_0
public static PropertyBuilder<DateOnly> HasDateOnlyConversion<DateOnly>(this PropertyBuilder<DateOnly> propertyBuilder)
=> propertyBuilder.HasConversion<DateOnlyConverter, DateOnlyComparer>();

public static PropertyBuilder<TimeOnly> HasTimeOnlyConversion<TimeOnly>(this PropertyBuilder<TimeOnly> propertyBuilder)
=> propertyBuilder.HasConversion<TimeOnlyConverter, TimeOnlyComparer>();
#endif

public static PropertyBuilder<T?> HasJsonConversion<T>(this PropertyBuilder<T?> propertyBuilder, JsonSerializerOptions? jsonSerializerOptions = null, bool useUtcDate = false, bool serializeEnumAsString = false)
{
Expand Down
81 changes: 52 additions & 29 deletions src/TinyHelpers.EntityFrameworkCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,75 @@ A collection of helper methods and classes for Entity Framework Core that I use

The library is available on [NuGet](https://www.nuget.org/packages/TinyHelpers.EntityFrameworkCore). Just search for *TinyHelpers.EntityFrameworkCore* in the **Package Manager GUI** or run the following command in the **.NET CLI**:

dotnet add package TinyHelpers.EntityFrameworkCore
```shell
dotnet add package TinyHelpers.EntityFrameworkCore
```

### Usage

#### Converters

The library provides some [Value Converters](https://docs.microsoft.com/ef/core/modeling/value-conversions) to handle data types that are not natively supported by Entity Framework Core. They can be explicitly used calling the [HasConversion](https://docs.microsoft.com/dotnet/api/microsoft.entityframeworkcore.metadata.builders.propertybuilder.hasconversion) method, or automatically via some extension methods:

// using TinyHelpers.EntityFrameworkCore.Extensions;
protected override void OnModelCreating(ModelBuilder modelBuilder)
```csharp
// using TinyHelpers.EntityFrameworkCore.Extensions;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>(builder =>
{
modelBuilder.Entity<Post>(builder =>
{
// Date is a DateOnly property (.NET 6 or higher).
builder.Property(x => x.Date).HasDateOnlyConversion();

// Time is a TimeOnly property (.NET 6 or higher).
builder.Property(x => x.Time).HasTimeOnlyConversion();

// Comments is a complex type, this Converter will automatically JSON-de/serialize it
// in a string column.
builder.Property(x => x.Comments).HasJsonConversion();
});
}
builder.Property(x => x.Title).HasMaxLength(80).IsRequired();
builder.Property(x => x.Content).IsRequired();

// Date is a DateOnly property (.NET 6 or 7).
//builder.Property(x => x.Date).HasDateOnlyConversion();

// Time is a TimeOnly property (.NET 6 or 7).
//builder.Property(x => x.Time).HasTimeOnlyConversion();

/* JSON SUPPORT */

// For .NET 6:
// Reviews is a complex type, this Converter will automatically JSON-de/serialize it
// in a string column.
// builder.Property(x => x.Reviews).HasJsonConversion();

// For .NET 7 or higher:
builder.OwnsMany(x => x.Reviews).ToJson();

/* COLLECTION OF PRIMITIVE TYPES */

// For .NET 6 and 7:
//builder.Property(x => x.Authors).HasArrayConversion();

// For .NET 8
// The support for collection of primitive types is built-in.
});
}
```

#### Query Filters

The library provides an extension method that allows to apply a global [Query Filter](https://docs.microsoft.com/ef/core/querying/filters) to entities:

public abstract class DeletableEntity
{
public bool IsDeleted { get; set; }
}
```csharp
public abstract class DeletableEntity
{
public bool IsDeleted { get; set; }
}

public class Person : DeletableEntity { }
public class Person : DeletableEntity { }

public class City: DeletableEntity { }
public class City: DeletableEntity { }

public class PhoneNumber : DeletableEntity { }
public class PhoneNumber : DeletableEntity { }

// using TinyHelpers.EntityFrameworkCore.Extensions;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Apply a filter to all the entities that inherit (directly or indirectly) from DeletableEntity.
modelBuilder.ApplyQueryFilter<DeletableEntity>(e => !e.IsDeleted);
}
// using TinyHelpers.EntityFrameworkCore.Extensions;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Apply a filter to all the entities that inherit (directly or indirectly) from DeletableEntity.
modelBuilder.ApplyQueryFilter<DeletableEntity>(e => !e.IsDeleted);
}
```

### Contributing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -21,15 +21,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TinyHelpers" Version="2.0.35" />
<PackageReference Include="TinyHelpers" Version="3.0.2" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.21" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.25" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.14" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/TinyHelpers.EntityFrameworkCore/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "2.0",
"version": "3.0",
"publicReleaseRefSpec": [
"^refs/heads/master$" // we release out of master
],
Expand Down
Loading