Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't persist keys for embedded entities #16779

Merged
merged 1 commit into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class CosmosDbContextOptionsExtensions
/// <param name="accountEndpoint"> The account end-point to connect to. </param>
/// <param name="accountKey"> The account key. </param>
/// <param name="databaseName"> The database name. </param>
/// <param name="cosmosOptionsAction">An optional action to allow additional Cosmos-specific configuration.</param>
/// <param name="cosmosOptionsAction"> An optional action to allow additional Cosmos-specific configuration. </param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseCosmos<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
Expand All @@ -46,7 +46,7 @@ public static DbContextOptionsBuilder<TContext> UseCosmos<TContext>(
/// <param name="accountEndpoint"> The account end-point to connect to. </param>
/// <param name="accountKey"> The account key. </param>
/// <param name="databaseName"> The database name. </param>
/// <param name="cosmosOptionsAction">An optional action to allow additional Cosmos-specific configuration.</param>
/// <param name="cosmosOptionsAction"> An optional action to allow additional Cosmos-specific configuration. </param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder UseCosmos(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
Expand Down
23 changes: 22 additions & 1 deletion src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand All @@ -20,7 +21,27 @@ public static class CosmosPropertyExtensions
/// <returns> The property name used when targeting Cosmos. </returns>
public static string GetCosmosPropertyName([NotNull] this IProperty property) =>
(string)property[CosmosAnnotationNames.PropertyName]
?? property.Name;
?? GetDefaultPropertyName(property);

private static string GetDefaultPropertyName(IProperty property)
{
var entityType = property.DeclaringEntityType;
var ownership = entityType.FindOwnership();

if (ownership != null
&& !entityType.IsDocumentRoot())
{
var pk = property.FindContainingPrimaryKey();
if (pk != null
&& pk.Properties.Count == ownership.Properties.Count + (ownership.IsUnique ? 0 : 1)
&& ownership.Properties.All(fkProperty => pk.Properties.Contains(fkProperty)))
{
return "";
AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved
}
}

return property.Name;
}

/// <summary>
/// Sets the property name used when targeting Cosmos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -53,6 +55,7 @@ public static IServiceCollection AddEntityFrameworkCosmos([NotNull] this IServic
.TryAdd<IDbContextTransactionManager, CosmosTransactionManager>()
.TryAdd<IModelValidator, CosmosModelValidator>()
.TryAdd<IProviderConventionSetBuilder, CosmosConventionSetBuilder>()
.TryAdd<IValueGeneratorSelector, CosmosValueGeneratorSelector>()
.TryAdd<IDatabaseCreator, CosmosDatabaseCreator>()
.TryAdd<IQueryContextFactory, CosmosQueryContextFactory>()
.TryAdd<ITypeMappingSource, CosmosTypeMappingSource>()
Expand Down
19 changes: 13 additions & 6 deletions src/EFCore.Cosmos/Metadata/Conventions/StoreKeyConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Newtonsoft.Json.Linq;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
/// <summary>
/// <para>
/// A convention that adds the 'id' property - a key required by Azure Cosmos.
/// </para>
/// This convention also add the '__jObject' containing the JSON object returned by the store.
/// <para>
/// </para>
/// This convention also adds the '__jObject' containing the JSON object returned by the store.
/// </para>
/// </summary>
public class StoreKeyConvention :
IEntityTypeAddedConvention,
Expand Down Expand Up @@ -52,10 +53,6 @@ private static void Process(IConventionEntityTypeBuilder entityTypeBuilder)
var idProperty = entityTypeBuilder.Property(typeof(string), IdPropertyName);
idProperty.HasValueGenerator((_, __) => new IdValueGenerator());
entityTypeBuilder.HasKey(new[] { idProperty.Metadata });

var jObjectProperty = entityTypeBuilder.Property(typeof(JObject), JObjectPropertyName);
jObjectProperty.ToJsonProperty("");
jObjectProperty.ValueGenerated(ValueGenerated.OnAddOrUpdate);
}
else
{
Expand All @@ -68,7 +65,17 @@ private static void Process(IConventionEntityTypeBuilder entityTypeBuilder)
entityType.Builder.HasNoKey(key);
}
}
}

if (entityType.BaseType == null
&& !entityType.IsKeyless)
{
var jObjectProperty = entityTypeBuilder.Property(typeof(JObject), JObjectPropertyName);
jObjectProperty.ToJsonProperty("");
jObjectProperty.ValueGenerated(ValueGenerated.OnAddOrUpdate);
}
else
{
var jObjectProperty = entityType.FindDeclaredProperty(JObjectPropertyName);
if (jObjectProperty != null)
{
Expand Down
19 changes: 0 additions & 19 deletions src/EFCore.Cosmos/Metadata/Internal/CosmosEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal
Expand All @@ -25,22 +23,5 @@ public static bool IsDocumentRoot(this IEntityType entityType)
=> entityType.BaseType?.IsDocumentRoot()
?? (!entityType.IsOwned()
|| entityType[CosmosAnnotationNames.ContainerName] != null);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string GetCosmosPartitionKeyStoreName([NotNull] this IEntityType entityType)
{
var name = entityType.GetCosmosPartitionKeyPropertyName();
if (name != null)
{
return entityType.FindProperty(name).GetCosmosPropertyName();
}

return CosmosClientWrapper.DefaultPartitionKey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ protected override Expression VisitMember(MemberExpression memberExpression)
throw new InvalidOperationException();
}

var navigationProjection = innerEntityProjection.BindMember(memberExpression.Member, innerExpression.Type, out var propertyBase);
var navigationProjection = innerEntityProjection.BindMember(
memberExpression.Member, innerExpression.Type, clientEval: true, out var propertyBase);

if (!(propertyBase is INavigation navigation)
|| !navigation.IsEmbedded())
Expand Down
Loading