mirror of
https://github.com/fergalmoran/EFCore.NamingConventions.git
synced 2025-12-22 01:28:13 +00:00
Check name for null before attempting to rewrite
And annotate the plugin for nullability. Fixes #179
This commit is contained in:
@@ -468,6 +468,24 @@ public class NameRewritingConventionTest
|
|||||||
Assert.Null(entityType.GetTableName());
|
Assert.Null(entityType.GetTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Foreign_key_without_name_because_over_view()
|
||||||
|
{
|
||||||
|
var model = BuildModel(b =>
|
||||||
|
{
|
||||||
|
b.Entity<Blog>();
|
||||||
|
b.Entity<Post>().ToView("posts_view");
|
||||||
|
b.Entity<Post>().HasOne(x => x.Blog).WithMany().HasForeignKey(x => x.BlogId);
|
||||||
|
});
|
||||||
|
|
||||||
|
var postEntityType = model.FindEntityType(typeof(Post))!;
|
||||||
|
Assert.Null(postEntityType.GetTableName());
|
||||||
|
Assert.Collection(
|
||||||
|
postEntityType.GetForeignKeys().Select(fk => fk.GetConstraintName()),
|
||||||
|
Assert.Null,
|
||||||
|
Assert.Null);
|
||||||
|
}
|
||||||
|
|
||||||
private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo culture = null)
|
private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo culture = null)
|
||||||
=> BuildModel(builderAction, culture).GetEntityTypes().Single();
|
=> BuildModel(builderAction, culture).GetEntityTypes().Single();
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore;
|
|||||||
internal static class Check
|
internal static class Check
|
||||||
{
|
{
|
||||||
[ContractAnnotation("value:null => halt")]
|
[ContractAnnotation("value:null => halt")]
|
||||||
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName)
|
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] string parameterName)
|
||||||
{
|
{
|
||||||
if (ReferenceEquals(value, null))
|
if (ReferenceEquals(value, null))
|
||||||
{
|
{
|
||||||
@@ -27,8 +27,8 @@ internal static class Check
|
|||||||
[ContractAnnotation("value:null => halt")]
|
[ContractAnnotation("value:null => halt")]
|
||||||
public static T NotNull<T>(
|
public static T NotNull<T>(
|
||||||
[NoEnumeration] T value,
|
[NoEnumeration] T value,
|
||||||
[InvokerParameterName] [NotNull] string parameterName,
|
[InvokerParameterName] string parameterName,
|
||||||
[NotNull] string propertyName)
|
string propertyName)
|
||||||
{
|
{
|
||||||
if (ReferenceEquals(value, null))
|
if (ReferenceEquals(value, null))
|
||||||
{
|
{
|
||||||
@@ -42,7 +42,7 @@ internal static class Check
|
|||||||
}
|
}
|
||||||
|
|
||||||
[ContractAnnotation("value:null => halt")]
|
[ContractAnnotation("value:null => halt")]
|
||||||
public static IReadOnlyList<T> NotEmpty<T>(IReadOnlyList<T> value, [InvokerParameterName] [NotNull] string parameterName)
|
public static IReadOnlyList<T> NotEmpty<T>(IReadOnlyList<T> value, [InvokerParameterName] string parameterName)
|
||||||
{
|
{
|
||||||
NotNull(value, parameterName);
|
NotNull(value, parameterName);
|
||||||
|
|
||||||
@@ -57,32 +57,16 @@ internal static class Check
|
|||||||
}
|
}
|
||||||
|
|
||||||
[ContractAnnotation("value:null => halt")]
|
[ContractAnnotation("value:null => halt")]
|
||||||
public static string NotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
|
public static string NotEmpty(string? value, [InvokerParameterName] string parameterName)
|
||||||
{
|
{
|
||||||
Exception e = null;
|
if (value is null)
|
||||||
if (ReferenceEquals(value, null))
|
|
||||||
{
|
|
||||||
e = new ArgumentNullException(parameterName);
|
|
||||||
}
|
|
||||||
else if (value.Trim().Length == 0)
|
|
||||||
{
|
|
||||||
e = new ArgumentException(AbstractionsStrings.ArgumentIsEmpty(parameterName));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e != null)
|
|
||||||
{
|
{
|
||||||
NotEmpty(parameterName, nameof(parameterName));
|
NotEmpty(parameterName, nameof(parameterName));
|
||||||
|
|
||||||
throw e;
|
throw new ArgumentNullException(parameterName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
if (value.Trim().Length == 0)
|
||||||
}
|
|
||||||
|
|
||||||
public static string NullButNotEmpty([CanBeNull] string value, [InvokerParameterName] [NotNull] string parameterName)
|
|
||||||
{
|
|
||||||
if (!ReferenceEquals(value, null)
|
|
||||||
&& (value.Length == 0))
|
|
||||||
{
|
{
|
||||||
NotEmpty(parameterName, nameof(parameterName));
|
NotEmpty(parameterName, nameof(parameterName));
|
||||||
|
|
||||||
@@ -92,7 +76,19 @@ internal static class Check
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IReadOnlyList<T> HasNoNulls<T>(IReadOnlyList<T> value, [InvokerParameterName] [NotNull] string parameterName)
|
public static string? NullButNotEmpty(string? value, [InvokerParameterName] string parameterName)
|
||||||
|
{
|
||||||
|
if (value is not null && value.Length == 0)
|
||||||
|
{
|
||||||
|
NotEmpty(parameterName, nameof(parameterName));
|
||||||
|
|
||||||
|
throw new ArgumentException(AbstractionsStrings.ArgumentIsEmpty(parameterName));
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IReadOnlyList<T> HasNoNulls<T>(IReadOnlyList<T> value, [InvokerParameterName] string parameterName)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
NotNull(value, parameterName);
|
NotNull(value, parameterName);
|
||||||
@@ -106,4 +102,4 @@ internal static class Check
|
|||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<VersionPrefix>7.0.2</VersionPrefix>
|
<VersionPrefix>7.0.2</VersionPrefix>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
<SignAssembly>true</SignAssembly>
|
<SignAssembly>true</SignAssembly>
|
||||||
<AssemblyOriginatorKeyFile>../EFCore.NamingConventions.snk</AssemblyOriginatorKeyFile>
|
<AssemblyOriginatorKeyFile>../EFCore.NamingConventions.snk</AssemblyOriginatorKeyFile>
|
||||||
|
|||||||
@@ -36,19 +36,23 @@ public class NameRewritingConvention :
|
|||||||
// (see ProcessEntityTypeBaseTypeChanged). But we still have this check for safety.
|
// (see ProcessEntityTypeBaseTypeChanged). But we still have this check for safety.
|
||||||
if (entityType.BaseType is null)
|
if (entityType.BaseType is null)
|
||||||
{
|
{
|
||||||
entityTypeBuilder.ToTable(_namingNameRewriter.RewriteName(entityType.GetTableName()), entityType.GetSchema());
|
if (entityType.GetTableName() is { } tableName)
|
||||||
|
|
||||||
if (entityType.GetViewNameConfigurationSource() == ConfigurationSource.Convention)
|
|
||||||
{
|
{
|
||||||
entityTypeBuilder.ToView(_namingNameRewriter.RewriteName(entityType.GetViewName()), entityType.GetViewSchema());
|
entityTypeBuilder.ToTable(_namingNameRewriter.RewriteName(tableName), entityType.GetSchema());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entityType.GetViewNameConfigurationSource() == ConfigurationSource.Convention
|
||||||
|
&& entityType.GetViewName() is { } viewName)
|
||||||
|
{
|
||||||
|
entityTypeBuilder.ToView(_namingNameRewriter.RewriteName(viewName), entityType.GetViewSchema());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessEntityTypeBaseTypeChanged(
|
public void ProcessEntityTypeBaseTypeChanged(
|
||||||
IConventionEntityTypeBuilder entityTypeBuilder,
|
IConventionEntityTypeBuilder entityTypeBuilder,
|
||||||
IConventionEntityType newBaseType,
|
IConventionEntityType? newBaseType,
|
||||||
IConventionEntityType oldBaseType,
|
IConventionEntityType? oldBaseType,
|
||||||
IConventionContext<IConventionEntityType> context)
|
IConventionContext<IConventionEntityType> context)
|
||||||
{
|
{
|
||||||
var entityType = entityTypeBuilder.Metadata;
|
var entityType = entityTypeBuilder.Metadata;
|
||||||
@@ -56,7 +60,10 @@ public class NameRewritingConvention :
|
|||||||
if (newBaseType is null)
|
if (newBaseType is null)
|
||||||
{
|
{
|
||||||
// The entity is getting removed from a hierarchy. Set the (rewritten) TableName.
|
// The entity is getting removed from a hierarchy. Set the (rewritten) TableName.
|
||||||
entityTypeBuilder.ToTable(_namingNameRewriter.RewriteName(entityType.GetTableName()), entityType.GetSchema());
|
if (entityType.GetTableName() is { } tableName)
|
||||||
|
{
|
||||||
|
entityTypeBuilder.ToTable(_namingNameRewriter.RewriteName(tableName), entityType.GetSchema());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -83,7 +90,7 @@ public class NameRewritingConvention :
|
|||||||
// Unless it's a collection navigation, or the owned entity table name was explicitly set by the user, this triggers table
|
// Unless it's a collection navigation, or the owned entity table name was explicitly set by the user, this triggers table
|
||||||
// splitting, which means we need to undo rewriting which we've done previously.
|
// splitting, which means we need to undo rewriting which we've done previously.
|
||||||
if (foreignKey.IsOwnership
|
if (foreignKey.IsOwnership
|
||||||
&& !foreignKey.GetNavigation(false).IsCollection
|
&& !foreignKey.GetNavigation(false)!.IsCollection
|
||||||
&& ownedEntityType.GetTableNameConfigurationSource() != ConfigurationSource.Explicit)
|
&& ownedEntityType.GetTableNameConfigurationSource() != ConfigurationSource.Explicit)
|
||||||
{
|
{
|
||||||
// Reset the table name which we've set when the entity type was added.
|
// Reset the table name which we've set when the entity type was added.
|
||||||
@@ -106,8 +113,8 @@ public class NameRewritingConvention :
|
|||||||
public void ProcessEntityTypeAnnotationChanged(
|
public void ProcessEntityTypeAnnotationChanged(
|
||||||
IConventionEntityTypeBuilder entityTypeBuilder,
|
IConventionEntityTypeBuilder entityTypeBuilder,
|
||||||
string name,
|
string name,
|
||||||
IConventionAnnotation annotation,
|
IConventionAnnotation? annotation,
|
||||||
IConventionAnnotation oldAnnotation,
|
IConventionAnnotation? oldAnnotation,
|
||||||
IConventionContext<IConventionAnnotation> context)
|
IConventionContext<IConventionAnnotation> context)
|
||||||
{
|
{
|
||||||
var entityType = entityTypeBuilder.Metadata;
|
var entityType = entityTypeBuilder.Metadata;
|
||||||
@@ -116,7 +123,7 @@ public class NameRewritingConvention :
|
|||||||
// we're the one who set the table name back when the entity type was originally added. We now undo this as the entity type
|
// we're the one who set the table name back when the entity type was originally added. We now undo this as the entity type
|
||||||
// should only be mapped to the View/SqlQuery/Function.
|
// should only be mapped to the View/SqlQuery/Function.
|
||||||
if (name is RelationalAnnotationNames.ViewName or RelationalAnnotationNames.SqlQuery or RelationalAnnotationNames.FunctionName
|
if (name is RelationalAnnotationNames.ViewName or RelationalAnnotationNames.SqlQuery or RelationalAnnotationNames.FunctionName
|
||||||
&& annotation.Value is not null
|
&& annotation?.Value is not null
|
||||||
&& entityType.GetTableNameConfigurationSource() == ConfigurationSource.Convention)
|
&& entityType.GetTableNameConfigurationSource() == ConfigurationSource.Convention)
|
||||||
{
|
{
|
||||||
entityType.SetTableName(null);
|
entityType.SetTableName(null);
|
||||||
@@ -142,7 +149,10 @@ public class NameRewritingConvention :
|
|||||||
|
|
||||||
if (entityType.FindRowInternalForeignKeys(tableIdentifier).FirstOrDefault() is null && !isTPT)
|
if (entityType.FindRowInternalForeignKeys(tableIdentifier).FirstOrDefault() is null && !isTPT)
|
||||||
{
|
{
|
||||||
primaryKey.Builder.HasName(_namingNameRewriter.RewriteName(primaryKey.GetDefaultName()));
|
if (primaryKey.GetDefaultName() is { } primaryKeyName)
|
||||||
|
{
|
||||||
|
primaryKey.Builder.HasName(_namingNameRewriter.RewriteName(primaryKeyName));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -162,12 +172,18 @@ public class NameRewritingConvention :
|
|||||||
|
|
||||||
foreach (var foreignKey in entityType.GetForeignKeys())
|
foreach (var foreignKey in entityType.GetForeignKeys())
|
||||||
{
|
{
|
||||||
foreignKey.Builder.HasConstraintName(_namingNameRewriter.RewriteName(foreignKey.GetDefaultName()));
|
if (foreignKey.GetDefaultName() is { } foreignKeyName)
|
||||||
|
{
|
||||||
|
foreignKey.Builder.HasConstraintName(_namingNameRewriter.RewriteName(foreignKeyName));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var index in entityType.GetIndexes())
|
foreach (var index in entityType.GetIndexes())
|
||||||
{
|
{
|
||||||
index.Builder.HasDatabaseName(_namingNameRewriter.RewriteName(index.GetDefaultDatabaseName()));
|
if (index.GetDefaultDatabaseName() is { } indexName)
|
||||||
|
{
|
||||||
|
index.Builder.HasDatabaseName(_namingNameRewriter.RewriteName(indexName));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (annotation?.Value is not null
|
if (annotation?.Value is not null
|
||||||
@@ -178,7 +194,7 @@ public class NameRewritingConvention :
|
|||||||
|
|
||||||
// When the entity became owned, we prefixed all of its properties - we must now undo that.
|
// When the entity became owned, we prefixed all of its properties - we must now undo that.
|
||||||
foreach (var property in entityType.GetProperties()
|
foreach (var property in entityType.GetProperties()
|
||||||
.Except(entityType.FindPrimaryKey().Properties)
|
.Except(entityType.FindPrimaryKey()?.Properties ?? Array.Empty<IConventionProperty>())
|
||||||
.Where(p => p.Builder.CanSetColumnName(null)))
|
.Where(p => p.Builder.CanSetColumnName(null)))
|
||||||
{
|
{
|
||||||
RewriteColumnName(property.Builder);
|
RewriteColumnName(property.Builder);
|
||||||
@@ -186,9 +202,10 @@ public class NameRewritingConvention :
|
|||||||
|
|
||||||
// We previously rewrote the owned entity's primary key name, when the owned entity was still in table splitting.
|
// We previously rewrote the owned entity's primary key name, when the owned entity was still in table splitting.
|
||||||
// Now that its getting its own table, rewrite the primary key constraint name again.
|
// Now that its getting its own table, rewrite the primary key constraint name again.
|
||||||
if (entityType.FindPrimaryKey() is IConventionKey key)
|
if (entityType.FindPrimaryKey() is IConventionKey key
|
||||||
|
&& key.GetDefaultName() is { } keyName)
|
||||||
{
|
{
|
||||||
key.Builder.HasName(_namingNameRewriter.RewriteName(key.GetDefaultName()));
|
key.Builder.HasName(_namingNameRewriter.RewriteName(keyName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -196,15 +213,30 @@ public class NameRewritingConvention :
|
|||||||
public void ProcessForeignKeyAdded(
|
public void ProcessForeignKeyAdded(
|
||||||
IConventionForeignKeyBuilder relationshipBuilder,
|
IConventionForeignKeyBuilder relationshipBuilder,
|
||||||
IConventionContext<IConventionForeignKeyBuilder> context)
|
IConventionContext<IConventionForeignKeyBuilder> context)
|
||||||
=> relationshipBuilder.HasConstraintName(_namingNameRewriter.RewriteName(relationshipBuilder.Metadata.GetDefaultName()));
|
{
|
||||||
|
if (relationshipBuilder.Metadata.GetDefaultName() is { } constraintName)
|
||||||
|
{
|
||||||
|
relationshipBuilder.HasConstraintName(_namingNameRewriter.RewriteName(constraintName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void ProcessKeyAdded(IConventionKeyBuilder keyBuilder, IConventionContext<IConventionKeyBuilder> context)
|
public void ProcessKeyAdded(IConventionKeyBuilder keyBuilder, IConventionContext<IConventionKeyBuilder> context)
|
||||||
=> keyBuilder.HasName(_namingNameRewriter.RewriteName(keyBuilder.Metadata.GetName()));
|
{
|
||||||
|
if (keyBuilder.Metadata.GetName() is { } keyName)
|
||||||
|
{
|
||||||
|
keyBuilder.HasName(_namingNameRewriter.RewriteName(keyName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void ProcessIndexAdded(
|
public void ProcessIndexAdded(
|
||||||
IConventionIndexBuilder indexBuilder,
|
IConventionIndexBuilder indexBuilder,
|
||||||
IConventionContext<IConventionIndexBuilder> context)
|
IConventionContext<IConventionIndexBuilder> context)
|
||||||
=> indexBuilder.HasDatabaseName(_namingNameRewriter.RewriteName(indexBuilder.Metadata.GetDefaultDatabaseName()));
|
{
|
||||||
|
if (indexBuilder.Metadata.GetDefaultDatabaseName() is { } indexName)
|
||||||
|
{
|
||||||
|
indexBuilder.HasDatabaseName(_namingNameRewriter.RewriteName(indexName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// EF Core's <see cref="SharedTableConvention" /> runs at model finalization time, and adds entity type prefixes to
|
/// EF Core's <see cref="SharedTableConvention" /> runs at model finalization time, and adds entity type prefixes to
|
||||||
@@ -269,7 +301,10 @@ public class NameRewritingConvention :
|
|||||||
var baseColumnName = StoreObjectIdentifier.Create(property.DeclaringEntityType, StoreObjectType.Table) is { } tableIdentifier
|
var baseColumnName = StoreObjectIdentifier.Create(property.DeclaringEntityType, StoreObjectType.Table) is { } tableIdentifier
|
||||||
? property.GetDefaultColumnName(tableIdentifier)
|
? property.GetDefaultColumnName(tableIdentifier)
|
||||||
: property.GetDefaultColumnName();
|
: property.GetDefaultColumnName();
|
||||||
propertyBuilder.HasColumnName(_namingNameRewriter.RewriteName(baseColumnName));
|
if (baseColumnName is not null)
|
||||||
|
{
|
||||||
|
propertyBuilder.HasColumnName(_namingNameRewriter.RewriteName(baseColumnName));
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var storeObjectType in _storeObjectTypes)
|
foreach (var storeObjectType in _storeObjectTypes)
|
||||||
{
|
{
|
||||||
@@ -279,10 +314,10 @@ public class NameRewritingConvention :
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (property.GetColumnNameConfigurationSource(identifier.Value) == ConfigurationSource.Convention)
|
if (property.GetColumnNameConfigurationSource(identifier.Value) == ConfigurationSource.Convention
|
||||||
|
&& property.GetColumnName(identifier.Value) is { } columnName)
|
||||||
{
|
{
|
||||||
propertyBuilder.HasColumnName(
|
propertyBuilder.HasColumnName(_namingNameRewriter.RewriteName(columnName), identifier.Value);
|
||||||
_namingNameRewriter.RewriteName(property.GetColumnName(identifier.Value)), identifier.Value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ namespace EFCore.NamingConventions.Internal;
|
|||||||
public class NamingConventionSetPlugin : IConventionSetPlugin
|
public class NamingConventionSetPlugin : IConventionSetPlugin
|
||||||
{
|
{
|
||||||
private readonly IDbContextOptions _options;
|
private readonly IDbContextOptions _options;
|
||||||
public NamingConventionSetPlugin([NotNull] IDbContextOptions options) => _options = options;
|
public NamingConventionSetPlugin(IDbContextOptions options) => _options = options;
|
||||||
|
|
||||||
public ConventionSet ModifyConventions(ConventionSet conventionSet)
|
public ConventionSet ModifyConventions(ConventionSet conventionSet)
|
||||||
{
|
{
|
||||||
var extension = _options.FindExtension<NamingConventionsOptionsExtension>();
|
var extension = _options.FindExtension<NamingConventionsOptionsExtension>()!;
|
||||||
var namingStyle = extension.NamingConvention;
|
var namingStyle = extension.NamingConvention;
|
||||||
var culture = extension.Culture;
|
var culture = extension.Culture;
|
||||||
if (namingStyle == NamingConvention.None)
|
if (namingStyle == NamingConvention.None)
|
||||||
@@ -44,4 +44,4 @@ public class NamingConventionSetPlugin : IConventionSetPlugin
|
|||||||
|
|
||||||
return conventionSet;
|
return conventionSet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,17 @@ using System.Globalization;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using JetBrains.Annotations;
|
|
||||||
|
|
||||||
namespace EFCore.NamingConventions.Internal;
|
namespace EFCore.NamingConventions.Internal;
|
||||||
|
|
||||||
public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
||||||
{
|
{
|
||||||
private DbContextOptionsExtensionInfo _info;
|
private DbContextOptionsExtensionInfo? _info;
|
||||||
private NamingConvention _namingConvention;
|
private NamingConvention _namingConvention;
|
||||||
private CultureInfo _culture;
|
private CultureInfo? _culture;
|
||||||
|
|
||||||
public NamingConventionsOptionsExtension() {}
|
public NamingConventionsOptionsExtension() {}
|
||||||
protected NamingConventionsOptionsExtension([NotNull] NamingConventionsOptionsExtension copyFrom)
|
protected NamingConventionsOptionsExtension(NamingConventionsOptionsExtension copyFrom)
|
||||||
{
|
{
|
||||||
_namingConvention = copyFrom._namingConvention;
|
_namingConvention = copyFrom._namingConvention;
|
||||||
_culture = copyFrom._culture;
|
_culture = copyFrom._culture;
|
||||||
@@ -26,7 +25,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
protected virtual NamingConventionsOptionsExtension Clone() => new(this);
|
protected virtual NamingConventionsOptionsExtension Clone() => new(this);
|
||||||
|
|
||||||
internal virtual NamingConvention NamingConvention => _namingConvention;
|
internal virtual NamingConvention NamingConvention => _namingConvention;
|
||||||
internal virtual CultureInfo Culture => _culture;
|
internal virtual CultureInfo? Culture => _culture;
|
||||||
|
|
||||||
public virtual NamingConventionsOptionsExtension WithoutNaming()
|
public virtual NamingConventionsOptionsExtension WithoutNaming()
|
||||||
{
|
{
|
||||||
@@ -35,7 +34,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual NamingConventionsOptionsExtension WithSnakeCaseNamingConvention(CultureInfo culture = null)
|
public virtual NamingConventionsOptionsExtension WithSnakeCaseNamingConvention(CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
var clone = Clone();
|
var clone = Clone();
|
||||||
clone._namingConvention = NamingConvention.SnakeCase;
|
clone._namingConvention = NamingConvention.SnakeCase;
|
||||||
@@ -43,7 +42,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual NamingConventionsOptionsExtension WithLowerCaseNamingConvention(CultureInfo culture = null)
|
public virtual NamingConventionsOptionsExtension WithLowerCaseNamingConvention(CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
var clone = Clone();
|
var clone = Clone();
|
||||||
clone._namingConvention = NamingConvention.LowerCase;
|
clone._namingConvention = NamingConvention.LowerCase;
|
||||||
@@ -51,7 +50,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual NamingConventionsOptionsExtension WithUpperCaseNamingConvention(CultureInfo culture = null)
|
public virtual NamingConventionsOptionsExtension WithUpperCaseNamingConvention(CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
var clone = Clone();
|
var clone = Clone();
|
||||||
clone._namingConvention = NamingConvention.UpperCase;
|
clone._namingConvention = NamingConvention.UpperCase;
|
||||||
@@ -59,7 +58,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual NamingConventionsOptionsExtension WithUpperSnakeCaseNamingConvention(CultureInfo culture = null)
|
public virtual NamingConventionsOptionsExtension WithUpperSnakeCaseNamingConvention(CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
var clone = Clone();
|
var clone = Clone();
|
||||||
clone._namingConvention = NamingConvention.UpperSnakeCase;
|
clone._namingConvention = NamingConvention.UpperSnakeCase;
|
||||||
@@ -67,7 +66,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual NamingConventionsOptionsExtension WithCamelCaseNamingConvention(CultureInfo culture = null)
|
public virtual NamingConventionsOptionsExtension WithCamelCaseNamingConvention(CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
var clone = Clone();
|
var clone = Clone();
|
||||||
clone._namingConvention = NamingConvention.CamelCase;
|
clone._namingConvention = NamingConvention.CamelCase;
|
||||||
@@ -82,7 +81,7 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
|
|
||||||
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
|
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
|
||||||
{
|
{
|
||||||
private string _logFragment;
|
private string? _logFragment;
|
||||||
|
|
||||||
public ExtensionInfo(IDbContextOptionsExtension extension) : base(extension) {}
|
public ExtensionInfo(IDbContextOptionsExtension extension) : base(extension) {}
|
||||||
|
|
||||||
@@ -145,4 +144,4 @@ public class NamingConventionsOptionsExtension : IDbContextOptionsExtension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,6 @@ public class SnakeCaseNameRewriter : INameRewriter
|
|||||||
|
|
||||||
public virtual string RewriteName(string name)
|
public virtual string RewriteName(string name)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(name))
|
|
||||||
{
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
var builder = new StringBuilder(name.Length + Math.Min(2, name.Length / 5));
|
var builder = new StringBuilder(name.Length + Math.Min(2, name.Length / 5));
|
||||||
var previousCategory = default(UnicodeCategory?);
|
var previousCategory = default(UnicodeCategory?);
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ namespace Microsoft.EntityFrameworkCore;
|
|||||||
public static class NamingConventionsExtensions
|
public static class NamingConventionsExtensions
|
||||||
{
|
{
|
||||||
public static DbContextOptionsBuilder UseSnakeCaseNamingConvention(
|
public static DbContextOptionsBuilder UseSnakeCaseNamingConvention(
|
||||||
[NotNull] this DbContextOptionsBuilder optionsBuilder , CultureInfo culture = null)
|
this DbContextOptionsBuilder optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
||||||
|
|
||||||
@@ -23,12 +24,13 @@ public static class NamingConventionsExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DbContextOptionsBuilder<TContext> UseSnakeCaseNamingConvention<TContext>(
|
public static DbContextOptionsBuilder<TContext> UseSnakeCaseNamingConvention<TContext>(
|
||||||
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder , CultureInfo culture = null)
|
this DbContextOptionsBuilder<TContext> optionsBuilder , CultureInfo? culture = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
=> (DbContextOptionsBuilder<TContext>)UseSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
=> (DbContextOptionsBuilder<TContext>)UseSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
||||||
|
|
||||||
public static DbContextOptionsBuilder UseLowerCaseNamingConvention(
|
public static DbContextOptionsBuilder UseLowerCaseNamingConvention(
|
||||||
[NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
||||||
|
|
||||||
@@ -42,12 +44,14 @@ public static class NamingConventionsExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DbContextOptionsBuilder<TContext> UseLowerCaseNamingConvention<TContext>(
|
public static DbContextOptionsBuilder<TContext> UseLowerCaseNamingConvention<TContext>(
|
||||||
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
=> (DbContextOptionsBuilder<TContext>)UseLowerCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder ,culture);
|
=> (DbContextOptionsBuilder<TContext>)UseLowerCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder ,culture);
|
||||||
|
|
||||||
public static DbContextOptionsBuilder UseUpperCaseNamingConvention(
|
public static DbContextOptionsBuilder UseUpperCaseNamingConvention(
|
||||||
[NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
||||||
|
|
||||||
@@ -61,12 +65,14 @@ public static class NamingConventionsExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DbContextOptionsBuilder<TContext> UseUpperCaseNamingConvention<TContext>(
|
public static DbContextOptionsBuilder<TContext> UseUpperCaseNamingConvention<TContext>(
|
||||||
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
=> (DbContextOptionsBuilder<TContext>)UseUpperCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
=> (DbContextOptionsBuilder<TContext>)UseUpperCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
||||||
|
|
||||||
public static DbContextOptionsBuilder UseUpperSnakeCaseNamingConvention(
|
public static DbContextOptionsBuilder UseUpperSnakeCaseNamingConvention(
|
||||||
[NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
||||||
|
|
||||||
@@ -80,12 +86,14 @@ public static class NamingConventionsExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DbContextOptionsBuilder<TContext> UseUpperSnakeCaseNamingConvention<TContext>(
|
public static DbContextOptionsBuilder<TContext> UseUpperSnakeCaseNamingConvention<TContext>(
|
||||||
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
=> (DbContextOptionsBuilder<TContext>)UseUpperSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
=> (DbContextOptionsBuilder<TContext>)UseUpperSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
||||||
|
|
||||||
public static DbContextOptionsBuilder UseCamelCaseNamingConvention(
|
public static DbContextOptionsBuilder UseCamelCaseNamingConvention(
|
||||||
[NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
{
|
{
|
||||||
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
|
||||||
|
|
||||||
@@ -99,7 +107,8 @@ public static class NamingConventionsExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DbContextOptionsBuilder<TContext> UseCamelCaseNamingConvention<TContext>(
|
public static DbContextOptionsBuilder<TContext> UseCamelCaseNamingConvention<TContext>(
|
||||||
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
|
this DbContextOptionsBuilder<TContext> optionsBuilder,
|
||||||
|
CultureInfo? culture = null)
|
||||||
where TContext : DbContext
|
where TContext : DbContext
|
||||||
=> (DbContextOptionsBuilder<TContext>)UseCamelCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
=> (DbContextOptionsBuilder<TContext>)UseCamelCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user