-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82847c7
commit 2d56999
Showing
8 changed files
with
124 additions
and
137 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
src/EFCore.Abstractions/EntityTypeConfigurationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
using System; | ||
|
||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
/// <summary> | ||
/// Specifies the configuration type for the entity type. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public sealed class EntityTypeConfigurationAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EntityTypeConfigurationAttribute" /> class. | ||
/// </summary> | ||
/// <param name="entityConfigurationType"> The IEntityTypeConfiguration<> type to use. </param> | ||
public EntityTypeConfigurationAttribute(Type entityConfigurationType) | ||
{ | ||
EntityTypeConfigurationType = entityConfigurationType; | ||
} | ||
|
||
/// <summary> | ||
/// Type of the entity type configuration. | ||
/// </summary> | ||
public Type EntityTypeConfigurationType { get; } | ||
} | ||
} |
85 changes: 0 additions & 85 deletions
85
src/EFCore/Metadata/Conventions/EntityConfigurationEntityTypeAttributeConvention.cs
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
src/EFCore/Metadata/Conventions/EntityTypeConfigurationEntityTypeAttributeConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions | ||
{ | ||
/// <summary> | ||
/// A convention that applies the entity type configuration specified in <see cref="EntityTypeConfigurationAttribute" />. | ||
/// </summary> | ||
public class EntityTypeConfigurationEntityTypeAttributeConvention : EntityTypeAttributeConventionBase<EntityTypeConfigurationAttribute> | ||
{ | ||
private static readonly MethodInfo _configureMethod = typeof(EntityTypeConfigurationEntityTypeAttributeConvention) | ||
.GetRequiredDeclaredMethod(nameof(Configure)); | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="EntityTypeConfigurationEntityTypeAttributeConvention" />. | ||
/// </summary> | ||
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param> | ||
public EntityTypeConfigurationEntityTypeAttributeConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies) | ||
: base(dependencies) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Called after an entity type is added to the model if it has an attribute. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"> The builder for the entity type. </param> | ||
/// <param name="attribute"> The attribute. </param> | ||
/// <param name="context"> Additional information associated with convention execution. </param> | ||
protected override void ProcessEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder, | ||
EntityTypeConfigurationAttribute attribute, | ||
IConventionContext<IConventionEntityTypeBuilder> context) | ||
{ | ||
var entityTypeConfigurationType = attribute.EntityTypeConfigurationType; | ||
|
||
if (!entityTypeConfigurationType.GetInterfaces().Any(x => | ||
x.IsGenericType | ||
&& x.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>) | ||
&& x.GenericTypeArguments[0] == entityTypeBuilder.Metadata.ClrType)) | ||
{ | ||
throw new InvalidOperationException(CoreStrings.InvalidEntityTypeConfigurationAttribute( | ||
entityTypeConfigurationType.ShortDisplayName(), entityTypeBuilder.Metadata.ShortName())); | ||
} | ||
|
||
_configureMethod.MakeGenericMethod(entityTypeBuilder.Metadata.ClrType) | ||
.Invoke(null, new object[] { entityTypeBuilder.Metadata, entityTypeConfigurationType }); | ||
} | ||
|
||
private static void Configure<TEntity>(IConventionEntityType entityType, Type entityTypeConfigurationType) | ||
where TEntity : class | ||
{ | ||
var entityTypeBuilder = new EntityTypeBuilder<TEntity>((IMutableEntityType)entityType); | ||
var entityTypeConfiguration = (IEntityTypeConfiguration<TEntity>)Activator.CreateInstance(entityTypeConfigurationType); | ||
entityTypeConfiguration.Configure(entityTypeBuilder); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters