Skip to content

Commit

Permalink
Add Microsoft.EntityFrameworkCore.Attributes assembly
Browse files Browse the repository at this point in the history
Change owned entity type configuration API.

Part of #9487
  • Loading branch information
AndriySvyryd committed Dec 14, 2017
1 parent 6d7e51c commit f94f7a1
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 41 deletions.
7 changes: 7 additions & 0 deletions EFCore.Runtime.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
tools\Resources.tt = tools\Resources.tt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCore.Attributes", "src\EFCore.Attributes\EFCore.Attributes.csproj", "{92C1AEF8-D4FC-4631-98B1-F16FB7103398}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -128,6 +130,10 @@ Global
{7583E3F0-8B29-4BEA-A55E-E8B66E6FD508}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7583E3F0-8B29-4BEA-A55E-E8B66E6FD508}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7583E3F0-8B29-4BEA-A55E-E8B66E6FD508}.Release|Any CPU.Build.0 = Release|Any CPU
{92C1AEF8-D4FC-4631-98B1-F16FB7103398}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92C1AEF8-D4FC-4631-98B1-F16FB7103398}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92C1AEF8-D4FC-4631-98B1-F16FB7103398}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92C1AEF8-D4FC-4631-98B1-F16FB7103398}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -150,6 +156,7 @@ Global
{07FA2B15-A6A5-4292-A096-7771FB32EEDA} = {258D5057-81B9-40EC-A872-D21E27452749}
{D3D0A8E8-EC2F-4E01-8650-8554E186A66F} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{7583E3F0-8B29-4BEA-A55E-E8B66E6FD508} = {258D5057-81B9-40EC-A872-D21E27452749}
{92C1AEF8-D4FC-4631-98B1-F16FB7103398} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EC8BCF1F-A206-4420-A292-3E3F2A4CDC54}
Expand Down
11 changes: 11 additions & 0 deletions EFCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.targets = Directory.Build.targets
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Attributes", "src\EFCore.Attributes\EFCore.Attributes.csproj", "{11B51A41-47CB-4EDB-9D8A-17095A65034A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -311,6 +313,14 @@ Global
{9511216F-EC10-480D-AA8D-F72312C96962}.Release|Any CPU.Build.0 = Release|Any CPU
{9511216F-EC10-480D-AA8D-F72312C96962}.ReleaseNoBenchmarks|Any CPU.ActiveCfg = Release|Any CPU
{9511216F-EC10-480D-AA8D-F72312C96962}.ReleaseNoBenchmarks|Any CPU.Build.0 = Release|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.DebugNoBenchmarks|Any CPU.ActiveCfg = Debug|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.DebugNoBenchmarks|Any CPU.Build.0 = Debug|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.Release|Any CPU.Build.0 = Release|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.ReleaseNoBenchmarks|Any CPU.ActiveCfg = Release|Any CPU
{11B51A41-47CB-4EDB-9D8A-17095A65034A}.ReleaseNoBenchmarks|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -345,6 +355,7 @@ Global
{8B289879-9CF1-4EFD-AD4B-781AEA94BDBE} = {8C6EFBE5-F0FB-48D1-B138-A651BB95C957}
{EA847C1D-EC25-442B-A940-460969577CD5} = {C41FD56B-38CD-40E3-89E6-071A6C58E36C}
{9511216F-EC10-480D-AA8D-F72312C96962} = {EA847C1D-EC25-442B-A940-460969577CD5}
{11B51A41-47CB-4EDB-9D8A-17095A65034A} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {285A5EB4-BCF4-40EB-B9E1-DF6DBCB5E705}
Expand Down
9 changes: 8 additions & 1 deletion samples/OracleProvider/OracleProvider.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.3
VisualStudioVersion = 15.0.27004.2010
MinimumVisualStudioVersion = 15.0.26730.03
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore", "..\..\src\EFCore\EFCore.csproj", "{715C38E9-B2F5-4DB2-8025-0C6492DEBDD4}"
EndProject
Expand All @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Relational.Specifica
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Specification.Tests", "..\..\src\EFCore.Specification.Tests\EFCore.Specification.Tests.csproj", "{1A73D95E-E8B5-4F96-908C-7B040E4F7AFE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCore.Attributes", "..\..\src\EFCore.Attributes\EFCore.Attributes.csproj", "{1561A725-B84F-4E3A-B316-1B3279DFDAF6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -47,6 +49,10 @@ Global
{1A73D95E-E8B5-4F96-908C-7B040E4F7AFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A73D95E-E8B5-4F96-908C-7B040E4F7AFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A73D95E-E8B5-4F96-908C-7B040E4F7AFE}.Release|Any CPU.Build.0 = Release|Any CPU
{1561A725-B84F-4E3A-B316-1B3279DFDAF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1561A725-B84F-4E3A-B316-1B3279DFDAF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1561A725-B84F-4E3A-B316-1B3279DFDAF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1561A725-B84F-4E3A-B316-1B3279DFDAF6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -56,6 +62,7 @@ Global
{6A25DF99-2615-46D8-9532-821764647EE1} = {57595541-E77D-477C-B48B-C69E061C11F9}
{07FA2B15-A6A5-4292-A096-7771FB32EEDA} = {57595541-E77D-477C-B48B-C69E061C11F9}
{1A73D95E-E8B5-4F96-908C-7B040E4F7AFE} = {57595541-E77D-477C-B48B-C69E061C11F9}
{1561A725-B84F-4E3A-B316-1B3279DFDAF6} = {57595541-E77D-477C-B48B-C69E061C11F9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {321B8079-2AC6-45FF-8D19-B2550F017448}
Expand Down
14 changes: 14 additions & 0 deletions src/EFCore.Attributes/EFCore.Attributes.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Provides attributes that are used to configure Entity Framework Core
</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<MinClientVersion>3.6</MinClientVersion>
<AssemblyName>Microsoft.EntityFrameworkCore.Attributes</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>..\EFCore.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions src/EFCore.Attributes/OwnedAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// Marks a type as owned. All references to this type will be configured as owned entity types.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class OwnedAttribute : Attribute
{
}
}
3 changes: 2 additions & 1 deletion src/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,7 @@ protected class Post
public Author Author { get; set; }
}

[ComplexType]
protected class PostDetails
{
public int Id { get; set; }
Expand Down Expand Up @@ -2085,7 +2086,7 @@ protected class Two
public byte[] Timestamp { get; set; }
}

[ComplexType]
[Owned]
protected class Details
{
public string Name { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions src/EFCore/EFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Microsoft.EntityFrameworkCore.DbSet
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersPackageVersion)" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EFCore.Attributes\EFCore.Attributes.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Properties\CoreStrings.Designer.tt">
<Generator>TextTemplatingFileGenerator</Generator>
Expand Down
42 changes: 42 additions & 0 deletions src/EFCore/Metadata/Builders/OwnedEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.ComponentModel;

namespace Microsoft.EntityFrameworkCore.Metadata.Builders
{
/// <summary>
/// <para>
/// Instances of this class are returned from methods when using the <see cref="ModelBuilder" /> API
/// and it is not designed to be directly constructed in your application code.
/// </para>
/// </summary>
public class OwnedEntityTypeBuilder
{
#region Hidden System.Object members

/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns> A string that represents the current object. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString() => base.ToString();

/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
/// <param name="obj"> The object to compare with the current object. </param>
/// <returns> true if the specified object is equal to the current object; otherwise, false. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => base.Equals(obj);

/// <summary>
/// Serves as the default hash function.
/// </summary>
/// <returns> A hash code for the current object. </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();

#endregion
}
}
14 changes: 14 additions & 0 deletions src/EFCore/Metadata/Builders/OwnedEntityTypeBuilder`.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Microsoft.EntityFrameworkCore.Metadata.Builders
{
/// <summary>
/// <para>
/// Instances of this class are returned from methods when using the <see cref="ModelBuilder" /> API
/// and it is not designed to be directly constructed in your application code.
/// </para>
/// </summary>
/// <typeparam name="T"> The entity type being configured. </typeparam>
// ReSharper disable once UnusedTypeParameter
public class OwnedEntityTypeBuilder<T> : OwnedEntityTypeBuilder
{
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal
Expand All @@ -10,13 +9,13 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class OwnedEntityTypeAttributeConvention : EntityTypeAttributeConvention<ComplexTypeAttribute>
public class OwnedEntityTypeAttributeConvention : EntityTypeAttributeConvention<OwnedAttribute>
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public override InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder, ComplexTypeAttribute attribute)
public override InternalEntityTypeBuilder Apply(InternalEntityTypeBuilder entityTypeBuilder, OwnedAttribute attribute)
=> (entityTypeBuilder.Metadata.HasClrType()
? entityTypeBuilder.ModelBuilder.OwnedEntity(entityTypeBuilder.Metadata.ClrType, ConfigurationSource.DataAnnotation)
: entityTypeBuilder.ModelBuilder.OwnedEntity(entityTypeBuilder.Metadata.Name, ConfigurationSource.DataAnnotation))
Expand Down
26 changes: 10 additions & 16 deletions src/EFCore/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,33 +240,27 @@ public virtual ModelBuilder ApplyConfiguration<TEntity>([NotNull] IEntityTypeCon
/// Marks an entity type as owned. All references to this type will be configured as
/// separate owned type instances.
/// </summary>
/// <typeparam name="TEntity"> The entity type to be configured. </typeparam>
public virtual void OwnedEntity<TEntity>()
where TEntity : class
=> Builder.OwnedEntity(typeof(TEntity), ConfigurationSource.Explicit);
/// <typeparam name="T"> The entity type to be configured. </typeparam>
public virtual OwnedEntityTypeBuilder<T> Owned<T>()
where T : class
{
Builder.OwnedEntity(typeof(T), ConfigurationSource.Explicit);

return null;
}

/// <summary>
/// Marks an entity type as owned. All references to this type will be configured as
/// separate owned type instances.
/// </summary>
/// <param name="type"> The entity type to be configured. </param>
public virtual void OwnedEntity([NotNull] Type type)
public virtual OwnedEntityTypeBuilder Owned([NotNull] Type type)
{
Check.NotNull(type, nameof(type));

Builder.OwnedEntity(type, ConfigurationSource.Explicit);
}

/// <summary>
/// Marks an entity type as owned. All references to this type will be configured as
/// separate owned type instances.
/// </summary>
/// <param name="name"> The name of the entity type to be configured. </param>
public virtual void OwnedEntity([NotNull] string name)
{
Check.NotEmpty(name, nameof(name));

Builder.OwnedEntity(name, ConfigurationSource.Explicit);
return null;
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion test/EFCore.Tests/ApiConsistencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ where type.GetTypeInfo().IsVisible
from method in type.GetMethods(PublicInstance)
where method.DeclaringType == type
&& method.ReturnType == typeof(void)
&& method.Name != nameof(ModelBuilder.OwnedEntity)
select type.Name + "." + method.Name;

Assert.Equal("", string.Join(Environment.NewLine, voidMethods));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public override TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TE
return this;
}

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity<TEntity>();
public override void Owned<TEntity>()
=> ModelBuilder.Owned<TEntity>();

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public override TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TE
return this;
}

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity<TEntity>();
public override void Owned<TEntity>()
=> ModelBuilder.Owned<TEntity>();

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public override TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TE
return this;
}

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity<TEntity>();
public override void Owned<TEntity>()
=> ModelBuilder.Owned<TEntity>();

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
4 changes: 2 additions & 2 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public override TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TE
return this;
}

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity<TEntity>();
public override void Owned<T>()
=> ModelBuilder.Owned<T>();

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,8 @@ public NonGenericStringTestEntityTypeBuilder Entity(Type type)
public NonGenericStringTestEntityTypeBuilder Entity(string name)
=> new NonGenericStringTestEntityTypeBuilder(ModelBuilder.Entity(name));

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity(typeof(TEntity));

public void OwnedEntity(string name)
=> ModelBuilder.OwnedEntity(name);
public override void Owned<TEntity>()
=> ModelBuilder.Owned(typeof(TEntity));

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
4 changes: 2 additions & 2 deletions test/EFCore.Tests/ModelBuilding/ModelBuilderNonGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public override TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TE
return this;
}

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity(typeof(TEntity));
public override void Owned<TEntity>()
=> ModelBuilder.Owned(typeof(TEntity));

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public override TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TE
return this;
}

public override void OwnedEntity<TEntity>()
=> ModelBuilder.OwnedEntity<TEntity>();
public override void Owned<TEntity>()
=> ModelBuilder.Owned<TEntity>();

public override TestModelBuilder Ignore<TEntity>()
{
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public abstract TestEntityTypeBuilder<TEntity> Entity<TEntity>()
where TEntity : class;


public abstract void OwnedEntity<TEntity>()
public abstract void Owned<TEntity>()
where TEntity : class;

public abstract TestModelBuilder Entity<TEntity>(Action<TestEntityTypeBuilder<TEntity>> buildAction)
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public virtual void Can_configure_all_ownerships_with_one_call()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.OwnedEntity<BookLabel>();
modelBuilder.Owned<BookLabel>();
modelBuilder.Entity<Book>().OwnsOne(b => b.Label);

modelBuilder.Validate();
Expand Down

0 comments on commit f94f7a1

Please sign in to comment.