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

dotnet optimize fails because of private JsonReaderWriters #2972

Closed
MinhMit opened this issue Nov 21, 2023 · 8 comments · Fixed by #3029
Closed

dotnet optimize fails because of private JsonReaderWriters #2972

MinhMit opened this issue Nov 21, 2023 · 8 comments · Fixed by #3029
Assignees
Labels
bug Something isn't working
Milestone

Comments

@MinhMit
Copy link

MinhMit commented Nov 21, 2023

Hi, I upgraded my project to .NET 8.0
And when I run optimize dbcontext, I received error:

'NpgsqlTimestampTzTypeMapping.NpgsqlJsonTimestampTzDateTimeOffsetReaderWriter' is inaccessible due to its protection level

Please see attact image

Screenshot 2023-11-21 214733

The command is:

ef dbcontext optimize -o OptimizedModels -n ProjectName.Infrastructure.OptimizedModels -c PrimaryApplicationDbContext

What can I do to fix this issue? Any workaround for this?

@JustArchi
Copy link

@roji got the same problem compiling my own project, says "Cannot access private class NpgsqlJsonTimestampTzDateTimeOffsetReaderWriter here". Seems like oversight and just making that class public or whatever would be sufficient, but of course you're expert here 👍

If you need reproduction, you can try:

[PrimaryKey(nameof(WalletType), nameof(AssetType), nameof(RecordedAt))]
[Table]
public sealed class AssetEntry {
	[Column]
	[Key]
	public int AssetType { get; set; }

	[Column]
	[Key]
	public DateTimeOffset RecordedAt { get; set; }

	[Column]
	[Key]
	public int WalletType { get; set; }
}

@roji roji self-assigned this Nov 21, 2023
@roji roji added the bug Something isn't working label Nov 21, 2023
@roji roji added this to the 8.0.1 milestone Nov 21, 2023
@roji
Copy link
Member

roji commented Nov 21, 2023

@JustArchi thanks for reporting, I'll take a look at this for 8.0.1.

@MinhMit
Copy link
Author

MinhMit commented Nov 22, 2023

How can we workaround to resolve this issue while waiting for the next release?

@eero-dev
Copy link

eero-dev commented Nov 22, 2023

Getting a similar issue, but with NpgsqlJsonTimestampTzDateTimeReaderWriter, so it's probably all related?

error CS0122: 'NpgsqlTimestampTzTypeMapping.NpgsqlJsonTimestampTzDateTimeReaderWriter' is inaccessible due to its prot
ection level

Optimized code:


           var endDate = runtimeEntityType.AddProperty(
               "EndDate",
               typeof(DateTime),
               propertyInfo: typeof(DateModel).GetProperty("EndDate", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
               fieldInfo: typeof(DateModel).GetField("<EndDate>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly),
               valueConverter: new UtcDateTimeConverter());
           endDate.TypeMapping = NpgsqlTimestampTzTypeMapping.Default.Clone(
               comparer: new ValueComparer<DateTime>(
                   (DateTime v1, DateTime v2) => v1.Equals(v2),
                   (DateTime v) => v.GetHashCode(),
                   (DateTime v) => v),
               keyComparer: new ValueComparer<DateTime>(
                   (DateTime v1, DateTime v2) => v1.Equals(v2),
                   (DateTime v) => v.GetHashCode(),
                   (DateTime v) => v),
               providerValueComparer: new ValueComparer<DateTime>(
                   (DateTime v1, DateTime v2) => v1.Equals(v2),
                   (DateTime v) => v.GetHashCode(),
                   (DateTime v) => v),
               converter: new ValueConverter<DateTime, DateTime>(
                   (DateTime v) => v,
                   (DateTime v) => DateTime.SpecifyKind(v, DateTimeKind.Utc)),
               jsonValueReaderWriter: new JsonConvertedValueReaderWriter<DateTime, DateTime>(
                   new NpgsqlTimestampTzTypeMapping.NpgsqlJsonTimestampTzDateTimeReaderWriter(),
                   new ValueConverter<DateTime, DateTime>(
                       (DateTime v) => v,
                       (DateTime v) => DateTime.SpecifyKind(v, DateTimeKind.Utc))));
           endDate.SetSentinelFromProviderValue(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
           endDate.AddAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.None);

😅
image

@roji
Copy link
Member

roji commented Nov 24, 2023

Everyone, I'm currently away and so unable to investigate this; I'm sure there's an actual issue here, but it'll take me a few days before I can investigate and fix it. You can probably expect a 8.0.1 to be released soon with the fix.

@springy76
Copy link

springy76 commented Dec 1, 2023

@MinhMit my workaround is to change the generated files so they look like this:

jsonValueReaderWriter: new NpgsqlTimestampTzTypeMapping(typeof(DateTimeOffset)).JsonValueReaderWriter);

which finally should return NpgsqlJsonTimestampTzDateTimeOffsetReaderWriter.Instance

@2Xpro-pop
Copy link

@MinhMit my workaround is to change the generated files so they look like this:

jsonValueReaderWriter: new NpgsqlTimestampTzTypeMapping(typeof(DateTimeOffset)).JsonValueReaderWriter);

which finally should return NpgsqlJsonTimestampTzDateTimeOffsetReaderWriter.Instance

Thank you very much.You helped me solve this problem, I wouldn't have done it without you.

@MinhMit
Copy link
Author

MinhMit commented Dec 8, 2023

@MinhMit my workaround is to change the generated files so they look like this:

jsonValueReaderWriter: new NpgsqlTimestampTzTypeMapping(typeof(DateTimeOffset)).JsonValueReaderWriter);

which finally should return NpgsqlJsonTimestampTzDateTimeOffsetReaderWriter.Instance

Thank you so much.

@roji roji changed the title .NET 8.0 Optimize DateTimeOffset dotnet optimize fails because of private JsonReaderWriter Dec 18, 2023
@roji roji changed the title dotnet optimize fails because of private JsonReaderWriter dotnet optimize fails because of private JsonReaderWriters Dec 18, 2023
roji added a commit to roji/efcore.pg that referenced this issue Dec 18, 2023
roji added a commit that referenced this issue Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants