diff --git a/src/EFCore.Abstractions/DbFunctionAttribute.cs b/src/EFCore.Abstractions/DbFunctionAttribute.cs index a64cbff5ff4..479e5702ec6 100644 --- a/src/EFCore.Abstractions/DbFunctionAttribute.cs +++ b/src/EFCore.Abstractions/DbFunctionAttribute.cs @@ -17,8 +17,6 @@ namespace Microsoft.EntityFrameworkCore public class DbFunctionAttribute : Attribute #pragma warning restore CA1813 // Avoid unsealed attributes { - private static readonly bool DefaultNullable = true; - private string _name; private string _schema; private bool _builtIn; @@ -36,14 +34,12 @@ public DbFunctionAttribute() /// /// The name of the function in the database. /// The schema of the function in the database. - /// The value indicating whether the database function is built-in or not. - public DbFunctionAttribute([NotNull] string name, [CanBeNull] string schema = null, bool builtIn = false) + public DbFunctionAttribute([NotNull] string name, [CanBeNull] string schema = null) { Check.NotEmpty(name, nameof(name)); _name = name; _schema = schema; - _builtIn = builtIn; } /// @@ -84,14 +80,13 @@ public virtual bool IsBuiltIn /// public virtual bool IsNullable { - get => _nullable ?? DefaultNullable; + get => _nullable ?? true; set => _nullable = value; } /// - /// Use this method if you want to know the nullability of - /// the database function or if it was not specified. + /// Checks whether has been explicitly set to a value. /// - public bool? GetIsNullable() => _nullable; + public bool IsNullableHasValue => _nullable.HasValue; } } diff --git a/src/EFCore.Abstractions/IndexAttribute.cs b/src/EFCore.Abstractions/IndexAttribute.cs index c0e0a083940..2f005a0100a 100644 --- a/src/EFCore.Abstractions/IndexAttribute.cs +++ b/src/EFCore.Abstractions/IndexAttribute.cs @@ -15,44 +15,47 @@ namespace Microsoft.EntityFrameworkCore [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public sealed class IndexAttribute : Attribute { - private static readonly bool DefaultIsUnique = false; private bool? _isUnique; + private string _name; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The properties which constitute the index, in order (there must be at least one). - public IndexAttribute(params string[] propertyNames) + public IndexAttribute([CanBeNull] params string[] propertyNames) { Check.NotEmpty(propertyNames, nameof(propertyNames)); Check.HasNoEmptyElements(propertyNames, nameof(propertyNames)); + PropertyNames = propertyNames.ToList(); } /// /// The properties which constitute the index, in order. /// - public List PropertyNames { get; } + public IReadOnlyList PropertyNames { get; } /// /// The name of the index. /// - public string Name { get; [param: NotNull] set; } - + public string Name + { + get => _name; + [param: NotNull] set => _name = Check.NotNull(value, nameof(value)); + } /// /// Whether the index is unique. /// public bool IsUnique { - get => _isUnique ?? DefaultIsUnique; + get => _isUnique ?? false; set => _isUnique = value; } /// - /// Use this method if you want to know the uniqueness of - /// the index or if it was not specified. + /// Checks whether has been explicitly set to a value. /// - public bool? GetIsUnique() => _isUnique; + public bool IsUniqueHasValue => _isUnique.HasValue; } } diff --git a/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs b/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs index 57e180f69c7..777ea37de53 100644 --- a/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs +++ b/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs @@ -137,7 +137,7 @@ public virtual IReadOnlyList GetTables(IEntityType entity { foreach (var et in entityType.GetDerivedTypesInclusive().Where(et => !et.IsAbstract())) { - var key = _useNameMatching ? (object)et.DisplayName() : et; + var key = _useNameMatching ? (object)et.FullName() : et; if (_tables.TryGetValue(key, out var table)) { data.Add(new InMemoryTableSnapshot(et, table.SnapshotRows())); @@ -218,7 +218,7 @@ private IInMemoryTable EnsureTable(IEntityType entityType) var entityTypes = entityType.GetAllBaseTypesInclusive(); foreach (var currentEntityType in entityTypes) { - var key = _useNameMatching ? (object)currentEntityType.DisplayName() : currentEntityType; + var key = _useNameMatching ? (object)currentEntityType.FullName() : currentEntityType; if (!_tables.TryGetValue(key, out var table)) { _tables.Add(key, table = _tableFactory.Create(currentEntityType, baseTable)); @@ -228,7 +228,7 @@ private IInMemoryTable EnsureTable(IEntityType entityType) } - return _tables[_useNameMatching ? (object)entityType.DisplayName() : entityType]; + return _tables[_useNameMatching ? (object)entityType.FullName() : entityType]; } } } diff --git a/src/EFCore.Relational/Metadata/Conventions/RelationalDbFunctionAttributeConvention.cs b/src/EFCore.Relational/Metadata/Conventions/RelationalDbFunctionAttributeConvention.cs index ebfba05b69c..cb92306a531 100644 --- a/src/EFCore.Relational/Metadata/Conventions/RelationalDbFunctionAttributeConvention.cs +++ b/src/EFCore.Relational/Metadata/Conventions/RelationalDbFunctionAttributeConvention.cs @@ -93,9 +93,9 @@ protected virtual void ProcessDbFunctionAdded( dbFunctionBuilder.IsBuiltIn(dbFunctionAttribute.IsBuiltIn, fromDataAnnotation: true); } - if (dbFunctionAttribute.GetIsNullable() is bool value) + if (dbFunctionAttribute.IsNullableHasValue) { - dbFunctionBuilder.IsNullable(value, fromDataAnnotation: true); + dbFunctionBuilder.IsNullable(dbFunctionAttribute.IsNullable, fromDataAnnotation: true); } } } diff --git a/src/EFCore/Metadata/Conventions/IndexAttributeConvention.cs b/src/EFCore/Metadata/Conventions/IndexAttributeConvention.cs index 7e91b99549e..c6a9493b584 100644 --- a/src/EFCore/Metadata/Conventions/IndexAttributeConvention.cs +++ b/src/EFCore/Metadata/Conventions/IndexAttributeConvention.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.Reflection; using JetBrains.Annotations; -using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; @@ -16,8 +15,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions /// /// A convention that configures database indexes based on the . /// - public class IndexAttributeConvention : IEntityTypeAddedConvention, - IEntityTypeBaseTypeChangedConvention, IModelFinalizingConvention + public class IndexAttributeConvention : IEntityTypeAddedConvention, IEntityTypeBaseTypeChangedConvention, IModelFinalizingConvention { /// /// Creates a new instance of . @@ -37,9 +35,7 @@ public IndexAttributeConvention([NotNull] ProviderConventionSetBuilderDependenci public virtual void ProcessEntityTypeAdded( IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext context) - { - CheckIndexAttributesAndEnsureIndex(entityTypeBuilder.Metadata, false); - } + => CheckIndexAttributesAndEnsureIndex(entityTypeBuilder.Metadata, false); /// public virtual void ProcessEntityTypeBaseTypeChanged( @@ -67,7 +63,7 @@ public virtual void ProcessModelFinalizing( } } - private void CheckIndexAttributesAndEnsureIndex( + private static void CheckIndexAttributesAndEnsureIndex( IConventionEntityType entityType, bool shouldThrow) { @@ -79,7 +75,7 @@ private void CheckIndexAttributesAndEnsureIndex( foreach (var indexAttribute in entityType.ClrType.GetCustomAttributes(true)) { - IConventionIndexBuilder indexBuilder = null; + IConventionIndexBuilder indexBuilder; if (!shouldThrow) { var indexProperties = new List(); @@ -125,18 +121,14 @@ private void CheckIndexAttributesAndEnsureIndex( { CheckIgnoredProperties(indexAttribute, entityType); } - else + else if (indexAttribute.IsUniqueHasValue) { - var shouldBeUnique = indexAttribute.GetIsUnique(); - if (shouldBeUnique.HasValue) - { - indexBuilder.IsUnique(shouldBeUnique.Value, fromDataAnnotation: true); - } + indexBuilder.IsUnique(indexAttribute.IsUnique, fromDataAnnotation: true); } } } - private void CheckIgnoredProperties( + private static void CheckIgnoredProperties( IndexAttribute indexAttribute, IConventionEntityType entityType) { @@ -152,20 +144,18 @@ private void CheckIgnoredProperties( indexAttribute.PropertyNames.Format(), propertyName)); } - else - { - throw new InvalidOperationException( - CoreStrings.NamedIndexDefinedOnIgnoredProperty( - indexAttribute.Name, - entityType.DisplayName(), - indexAttribute.PropertyNames.Format(), - propertyName)); - } + + throw new InvalidOperationException( + CoreStrings.NamedIndexDefinedOnIgnoredProperty( + indexAttribute.Name, + entityType.DisplayName(), + indexAttribute.PropertyNames.Format(), + propertyName)); } } } - private void CheckMissingProperties( + private static void CheckMissingProperties( IndexAttribute indexAttribute, IConventionEntityType entityType, InvalidOperationException innerException) @@ -184,16 +174,14 @@ private void CheckMissingProperties( propertyName), innerException); } - else - { - throw new InvalidOperationException( - CoreStrings.NamedIndexDefinedOnNonExistentProperty( - indexAttribute.Name, - entityType.DisplayName(), - indexAttribute.PropertyNames.Format(), - propertyName), - innerException); - } + + throw new InvalidOperationException( + CoreStrings.NamedIndexDefinedOnNonExistentProperty( + indexAttribute.Name, + entityType.DisplayName(), + indexAttribute.PropertyNames.Format(), + propertyName), + innerException); } } } diff --git a/src/Shared/Check.cs b/src/Shared/Check.cs index 962ebca2d74..9c73d9fd9e0 100644 --- a/src/Shared/Check.cs +++ b/src/Shared/Check.cs @@ -94,7 +94,6 @@ public static IReadOnlyList HasNoNulls(IReadOnlyList value, [InvokerPar return value; } - public static IReadOnlyList HasNoEmptyElements(IReadOnlyList value, [InvokerParameterName][NotNull] string parameterName) { NotNull(value, parameterName); diff --git a/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs b/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs index 8928fc86b6a..947999fd5f6 100644 --- a/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Query/UdfDbFunctionTestBase.cs @@ -126,7 +126,7 @@ public enum ReportingPeriod Fall } - [DbFunction(Name = "len", IsBuiltIn = true)] + [DbFunction("len", IsBuiltIn = true)] public static long MyCustomLengthStatic(string s) => throw new Exception(); public static bool IsDateStatic(string date) => throw new Exception(); public static int AddOneStatic(int num) => num + 1; diff --git a/test/EFCore.Relational.Tests/Metadata/DbFunctionMetadataTests.cs b/test/EFCore.Relational.Tests/Metadata/DbFunctionMetadataTests.cs index 0f5a0a21091..d1ac189cc74 100644 --- a/test/EFCore.Relational.Tests/Metadata/DbFunctionMetadataTests.cs +++ b/test/EFCore.Relational.Tests/Metadata/DbFunctionMetadataTests.cs @@ -207,7 +207,7 @@ public static int MethodA(string a, int b) throw new NotImplementedException(); } - [DbFunction(Schema = "bar", Name = "MethodFoo")] + [DbFunction("MethodFoo", "bar")] public static int MethodB(string c, int d) { throw new NotImplementedException();