Open juchom opened 3 years ago
If I change the signature to use the IRelationalDbContextOptionsBuilderInfrastructure
interface instead of RelationalDbContextOptionsBuilder<TBuilder, TExtension>
abstract class
public static class RelationalUlidDbContextOptionsBuilderExtensions
{
public static TBuilder UseUlidToString<TBuilder>(this TBuilder optionsBuilder)
where TBuilder : IRelationalDbContextOptionsBuilderInfrastructure
=> optionsBuilder.UseUlid(UlidStorageMode.String);
public static TBuilder UseUlidToBytes<TBuilder>(this TBuilder optionsBuilder)
where TBuilder : IRelationalDbContextOptionsBuilderInfrastructure
=> optionsBuilder.UseUlid(UlidStorageMode.Bytes);
internal static TBuilder UseUlid<TBuilder>(this TBuilder optionsBuilder, UlidStorageMode storageMode)
where TBuilder : IRelationalDbContextOptionsBuilderInfrastructure
{
if (optionsBuilder is null)
{
throw new ArgumentNullException(nameof(optionsBuilder));
}
var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder;
var extension = coreOptionsBuilder.Options.FindExtension<RelationalUlidOptionsExtension>();
if (extension is not null && extension.StorageMode != storageMode)
{
throw new InvalidOperationException("You are registering Ulid with two different storage mode");
}
if (extension is null)
{
extension = new RelationalUlidOptionsExtension(storageMode);
}
((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension);
return optionsBuilder;
}
}
Warnings are gone, but I'm not sure if this is a good solution...
Note for triage: it seems to me that SqlOptionsExtension
and similar should not be internal.
Note from triage: we believe the analyzer should be changed to not mark this as use of internal code.
Ask a question
I continue my work on integrating Ulid in EF Core, the code is pretty small and should work for every SQL Database.
Ulid is stored to char(26) or binary(16) nothing fancy.
I'm trying to avoid creating a package for SQL Server, Postgre, MySQL...
Include your code
If I do this in a Program.cs
Warnings
Is there a way to refactor this extension methods to avoid duplicating code
Writing the code like this for Sql Server removes the EF1001 warning
Writing the code like this for Postgre removes the EF1001 warning
As you can see, the only thing changing here is
SqlServerDbContextOptionsBuilder
toNpgsqlDbContextOptionsBuilder
.Include provider and version information
EF Core version: 6.0.0-rc2 Database provider: SqlServer & Npgsql Target framework: .NET 6.0.0-c2 Operating system: Windows 10 IDE: Visual Studio 2022