Skip to content

Commit

Permalink
Add complex types to Metadata
Browse files Browse the repository at this point in the history
Fixes #13947
  • Loading branch information
AndriySvyryd committed Jun 24, 2023
1 parent da6e296 commit cf37185
Show file tree
Hide file tree
Showing 327 changed files with 24,504 additions and 6,968 deletions.
7 changes: 4 additions & 3 deletions src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore;
Expand All @@ -26,11 +27,11 @@ public static string GetJsonPropertyName(this IReadOnlyProperty property)

private static string GetDefaultJsonPropertyName(IReadOnlyProperty property)
{
var entityType = property.DeclaringEntityType;
var ownership = entityType.FindOwnership();
var entityType = property.DeclaringType as IEntityType;
var ownership = entityType?.FindOwnership();

if (ownership != null
&& !entityType.IsDocumentRoot())
&& !entityType!.IsDocumentRoot())
{
var pk = property.FindContainingPrimaryKey();
if (pk != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ public virtual void ProcessEntityTypeAnnotationChanged(
/// <returns>The store value generation strategy to set for the given property.</returns>
protected override ValueGenerated? GetValueGenerated(IConventionProperty property)
{
var entityType = property.DeclaringEntityType;
var entityType = property.DeclaringType as IConventionEntityType;
var propertyType = property.ClrType.UnwrapNullableType();
if (propertyType == typeof(int))
if (propertyType == typeof(int)
&& entityType != null)
{
var ownership = entityType.FindOwnership();
if (ownership is { IsUnique: false }
Expand Down
8 changes: 5 additions & 3 deletions src/EFCore.Cosmos/Metadata/Conventions/StoreKeyConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ public virtual void ProcessPropertyAnnotationChanged(
&& (string?)annotation?.Value == IdPropertyJsonName
&& propertyBuilder.Metadata.Name != DefaultIdPropertyName)
{
var entityType = propertyBuilder.Metadata.DeclaringEntityType;
var declaringType = propertyBuilder.Metadata.DeclaringType;

var idProperty = entityType.FindProperty(DefaultIdPropertyName);
var idProperty = declaringType.FindProperty(DefaultIdPropertyName);
if (idProperty != null)
{
foreach (var key in idProperty.GetContainingKeys().ToList())
Expand All @@ -306,7 +306,9 @@ public virtual void ProcessPropertyAnnotationChanged(
}
}

ProcessIdProperty(entityType.Builder);
ProcessIdProperty(((declaringType as IConventionEntityType)
?? ((IConventionComplexType)declaringType).FundamentalEntityType)
.Builder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class CosmosPropertyExtensions
public static bool IsOrdinalKeyProperty(this IReadOnlyProperty property)
{
Check.DebugAssert(
property.DeclaringEntityType.IsOwned(), $"Expected {property.DeclaringEntityType.DisplayName()} to be owned.");
(property.DeclaringType as IEntityType)?.IsOwned() == true, $"Expected {property.DeclaringType.DisplayName()} to be owned.");
Check.DebugAssert(property.GetJsonPropertyName().Length == 0, $"Expected {property.Name} to be non-persisted.");

return property.FindContainingPrimaryKey() is IReadOnlyKey { Properties.Count: > 1 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,13 @@ private Expression CreateGetValueExpression(
var storeName = property.GetJsonPropertyName();
if (storeName.Length == 0)
{
var entityType = property.DeclaringEntityType;
if (!entityType.IsDocumentRoot())
var entityType = property.DeclaringType as IEntityType;
if (entityType == null
|| !entityType.IsDocumentRoot())
{
var ownership = entityType.FindOwnership();
if (!ownership.IsUnique
var ownership = entityType?.FindOwnership();
if (ownership != null
&& !ownership.IsUnique
&& property.IsOrdinalKeyProperty())
{
var readExpression = _ordinalParameterBindings[jObjectExpression];
Expand All @@ -621,8 +623,8 @@ private Expression CreateGetValueExpression(
if (_ownerMappings.TryGetValue(jObjectExpression, out var ownerInfo))
{
Check.DebugAssert(
principalProperty.DeclaringEntityType.IsAssignableFrom(ownerInfo.EntityType),
$"{principalProperty.DeclaringEntityType} is not assignable from {ownerInfo.EntityType}");
principalProperty.DeclaringType.IsAssignableFrom(ownerInfo.EntityType),
$"{principalProperty.DeclaringType} is not assignable from {ownerInfo.EntityType}");

ownerJObjectExpression = ownerInfo.JObjectExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public virtual Expression Update(Expression accessExpression)
/// </summary>
public virtual Expression BindProperty(IProperty property, bool clientEval)
{
if (!EntityType.IsAssignableFrom(property.DeclaringEntityType)
&& !property.DeclaringEntityType.IsAssignableFrom(EntityType))
if (!EntityType.IsAssignableFrom(property.DeclaringType)
&& !property.DeclaringType.IsAssignableFrom(EntityType))
{
throw new InvalidOperationException(
CosmosStrings.UnableToBindMemberToEntityProjection("property", property.Name, EntityType.DisplayName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.ValueGeneration;
public class IdValueGeneratorFactory : ValueGeneratorFactory
{
/// <inheritdoc />
public override ValueGenerator Create(IProperty property, IEntityType entityType)
public override ValueGenerator Create(IProperty property, ITypeBase entityType)
=> new IdValueGenerator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public CosmosValueGeneratorSelector(ValueGeneratorSelectorDependencies dependenc
/// 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>
protected override ValueGenerator? FindForType(IProperty property, IEntityType entityType, Type clrType)
protected override ValueGenerator? FindForType(IProperty property, ITypeBase typeBase, Type clrType)
{
if (property.GetJsonPropertyName() == ""
&& clrType == typeof(int))
{
return new TemporaryNumberValueGeneratorFactory().Create(property, entityType);
return new TemporaryNumberValueGeneratorFactory().Create(property, typeBase);
}

return base.FindForType(property, entityType, clrType);
return base.FindForType(property, typeBase, clrType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private static void UpdateOwnedTypes(IMutableEntityType entityType)
if (oldProperty is IConventionProperty conventionProperty
&& conventionProperty.GetConfigurationSource() == ConfigurationSource.Convention)
{
oldProperty.DeclaringEntityType.RemoveProperty(oldProperty);
oldProperty.DeclaringType.RemoveProperty(oldProperty);
}
}
}
Expand Down
Loading

0 comments on commit cf37185

Please sign in to comment.