-
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.
Cosmos: Add API to configure the container that a hierarchy is mapped…
… to, as well as the default container name Enable materializing derived types polymorphically Add exceptions for some unsupported cases
- Loading branch information
1 parent
31e0e41
commit b8ad5cc
Showing
55 changed files
with
1,609 additions
and
184 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
src/EFCore.Cosmos/CosmosCollectionOwnershipBuilderExtensions.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,53 @@ | ||
// 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 JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos | ||
{ | ||
/// <summary> | ||
/// Cosmos specific extension methods for <see cref="CollectionOwnershipBuilder" />. | ||
/// </summary> | ||
public static class CosmosCollectionOwnershipBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the container that the entity maps to when targeting Azure Cosmos. | ||
/// </summary> | ||
/// <param name="referenceOwnershipBuilder"> The builder for the entity type being configured. </param> | ||
/// <param name="name"> The name of the container. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static CollectionOwnershipBuilder ToContainer( | ||
[NotNull] this CollectionOwnershipBuilder referenceOwnershipBuilder, | ||
[CanBeNull] string name) | ||
{ | ||
Check.NotNull(referenceOwnershipBuilder, nameof(referenceOwnershipBuilder)); | ||
Check.NullButNotEmpty(name, nameof(name)); | ||
|
||
referenceOwnershipBuilder.GetInfrastructure<InternalEntityTypeBuilder>() | ||
.Cosmos(ConfigurationSource.Explicit) | ||
.ToContainer(name); | ||
|
||
return referenceOwnershipBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the container that the entity maps to when targeting Azure Cosmos. | ||
/// </summary> | ||
/// <typeparam name="TEntity"> The entity type being configured. </typeparam> | ||
/// <typeparam name="TDependentEntity"> The entity type that this relationship targets. </typeparam> | ||
/// <param name="referenceOwnershipBuilder"> The builder for the entity type being configured. </param> | ||
/// <param name="name"> The name of the container. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static CollectionOwnershipBuilder<TEntity, TDependentEntity> ToContainer<TEntity, TDependentEntity>( | ||
[NotNull] this CollectionOwnershipBuilder<TEntity, TDependentEntity> referenceOwnershipBuilder, | ||
[CanBeNull] string name) | ||
where TEntity : class | ||
where TDependentEntity : class | ||
=> (CollectionOwnershipBuilder<TEntity, TDependentEntity>)ToContainer((CollectionOwnershipBuilder)referenceOwnershipBuilder, name); | ||
} | ||
} |
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,52 @@ | ||
// 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 JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
/// <summary> | ||
/// Cosmos specific extension methods for <see cref="EntityTypeBuilder" />. | ||
/// </summary> | ||
public static class CosmosEntityTypeBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the container that the entity maps to when targeting Azure Cosmos. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param> | ||
/// <param name="name"> The name of the container. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static EntityTypeBuilder ToContainer( | ||
[NotNull] this EntityTypeBuilder entityTypeBuilder, | ||
[CanBeNull] string name) | ||
{ | ||
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder)); | ||
Check.NullButNotEmpty(name, nameof(name)); | ||
|
||
entityTypeBuilder.GetInfrastructure<InternalEntityTypeBuilder>() | ||
.Cosmos(ConfigurationSource.Explicit) | ||
.ToContainer(name); | ||
|
||
return entityTypeBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the container that the entity maps to when targeting Azure Cosmos. | ||
/// </summary> | ||
/// <typeparam name="TEntity"> The entity type being configured. </typeparam> | ||
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param> | ||
/// <param name="name"> The name of the container. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static EntityTypeBuilder<TEntity> ToContainer<TEntity>( | ||
[NotNull] this EntityTypeBuilder<TEntity> entityTypeBuilder, | ||
[CanBeNull] string name) | ||
where TEntity : class | ||
=> (EntityTypeBuilder<TEntity>)ToContainer((EntityTypeBuilder)entityTypeBuilder, name); | ||
} | ||
} |
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,34 @@ | ||
// 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 JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
public static class CosmosModelBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the default container name that will be used if no name | ||
/// is explicitly configured for an entity type. | ||
/// </summary> | ||
/// <param name="modelBuilder"> The model builder. </param> | ||
/// <param name="name"> The default container name. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static ModelBuilder HasDefaultContainerName( | ||
[NotNull] this ModelBuilder modelBuilder, | ||
[CanBeNull] string name) | ||
{ | ||
Check.NotNull(modelBuilder, nameof(modelBuilder)); | ||
Check.NullButNotEmpty(name, nameof(name)); | ||
|
||
modelBuilder.GetInfrastructure().Cosmos(ConfigurationSource.Explicit).HasDefaultContainerName(name); | ||
|
||
return modelBuilder; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/EFCore.Cosmos/CosmosReferenceOwnershipBuilderExtensions.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,53 @@ | ||
// 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 JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos | ||
{ | ||
/// <summary> | ||
/// Cosmos specific extension methods for <see cref="ReferenceOwnershipBuilder" />. | ||
/// </summary> | ||
public static class CosmosReferenceOwnershipBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the container that the entity maps to when targeting Azure Cosmos. | ||
/// </summary> | ||
/// <param name="referenceOwnershipBuilder"> The builder for the entity type being configured. </param> | ||
/// <param name="name"> The name of the container. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static ReferenceOwnershipBuilder ToContainer( | ||
[NotNull] this ReferenceOwnershipBuilder referenceOwnershipBuilder, | ||
[CanBeNull] string name) | ||
{ | ||
Check.NotNull(referenceOwnershipBuilder, nameof(referenceOwnershipBuilder)); | ||
Check.NullButNotEmpty(name, nameof(name)); | ||
|
||
referenceOwnershipBuilder.GetInfrastructure<InternalEntityTypeBuilder>() | ||
.Cosmos(ConfigurationSource.Explicit) | ||
.ToContainer(name); | ||
|
||
return referenceOwnershipBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the container that the entity maps to when targeting Azure Cosmos. | ||
/// </summary> | ||
/// <typeparam name="TEntity"> The entity type being configured. </typeparam> | ||
/// <typeparam name="TRelatedEntity"> The entity type that this relationship targets. </typeparam> | ||
/// <param name="referenceOwnershipBuilder"> The builder for the entity type being configured. </param> | ||
/// <param name="name"> The name of the container. </param> | ||
/// <returns> The same builder instance so that multiple calls can be chained. </returns> | ||
public static ReferenceOwnershipBuilder<TEntity, TRelatedEntity> ToContainer<TEntity, TRelatedEntity>( | ||
[NotNull] this ReferenceOwnershipBuilder<TEntity, TRelatedEntity> referenceOwnershipBuilder, | ||
[CanBeNull] string name) | ||
where TEntity : class | ||
where TRelatedEntity : class | ||
=> (ReferenceOwnershipBuilder<TEntity, TRelatedEntity>)ToContainer((ReferenceOwnershipBuilder)referenceOwnershipBuilder, name); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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 Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Builds the model for a given context. This default implementation builds the model by calling | ||
/// <see cref="DbContext.OnConfiguring(DbContextOptionsBuilder)" /> on the context. | ||
/// </para> | ||
/// <para> | ||
/// This type is typically used by database providers (and other extensions). It is generally | ||
/// not used in application code. | ||
/// </para> | ||
/// </summary> | ||
public class CosmosModelCustomizer : ModelCustomizer | ||
{ | ||
public CosmosModelCustomizer(ModelCustomizerDependencies dependencies) | ||
: base(dependencies) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Performs additional configuration of the model in addition to what is discovered by convention. This implementation | ||
/// builds the model for a given context by calling <see cref="DbContext.OnConfiguring(DbContextOptionsBuilder)" /> | ||
/// on the context. | ||
/// </para> | ||
/// </summary> | ||
/// <param name="modelBuilder"> | ||
/// The builder being used to construct the model. | ||
/// </param> | ||
/// <param name="context"> | ||
/// The context instance that the model is being created for. | ||
/// </param> | ||
public override void Customize(ModelBuilder modelBuilder, DbContext context) | ||
{ | ||
modelBuilder.GetInfrastructure().Cosmos(ConfigurationSource.Convention).HasDefaultContainerName(context.GetType().Name); | ||
|
||
base.Customize(modelBuilder, context); | ||
} | ||
} | ||
} |
File renamed without changes.
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
Oops, something went wrong.