|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; |
| 5 | +using Microsoft.EntityFrameworkCore.Metadata.Internal; |
| 6 | + |
| 7 | +namespace Microsoft.EntityFrameworkCore.ChangeTracking; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Provides access to change tracking information and operations for a given property of a complex type. |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// <para> |
| 14 | +/// Instances of this class are returned from methods when using the <see cref="ChangeTracker" /> API and it is |
| 15 | +/// not designed to be directly constructed in your application code. |
| 16 | +/// </para> |
| 17 | +/// <para> |
| 18 | +/// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 19 | +/// examples. |
| 20 | +/// </para> |
| 21 | +/// </remarks> |
| 22 | +public class ComplexPropertyEntry : MemberEntry |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to |
| 26 | + /// the same compatibility standards as public APIs. It may be changed or removed without notice in |
| 27 | + /// any release. You should only use it directly in your code with extreme caution and knowing that |
| 28 | + /// doing so can result in application failures when updating to a new Entity Framework Core release. |
| 29 | + /// </summary> |
| 30 | + [EntityFrameworkInternal] |
| 31 | + public ComplexPropertyEntry(InternalEntityEntry internalEntry, IComplexProperty complexProperty) |
| 32 | + : base(internalEntry, complexProperty) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets or sets a value indicating whether any of the properties of the complex type have been modified |
| 38 | + /// and should be updated in the database when <see cref="DbContext.SaveChanges()" /> is called. |
| 39 | + /// </summary> |
| 40 | + /// <remarks> |
| 41 | + /// <para> |
| 42 | + /// Setting this value causes all of the properties of the complex type to be marked as modified or not as appropriate. |
| 43 | + /// </para> |
| 44 | + /// <para> |
| 45 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 46 | + /// examples. |
| 47 | + /// </para> |
| 48 | + /// </remarks> |
| 49 | + public override bool IsModified |
| 50 | + { |
| 51 | + get => Metadata.ComplexType.GetFlattenedProperties().Any(property => InternalEntry.IsModified(property)); |
| 52 | + set |
| 53 | + { |
| 54 | + foreach (var property in Metadata.ComplexType.GetFlattenedProperties()) |
| 55 | + { |
| 56 | + InternalEntry.SetPropertyModified(property, isModified: value); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets the metadata that describes the facets of this property and how it maps to the database. |
| 63 | + /// </summary> |
| 64 | + public new virtual IComplexProperty Metadata |
| 65 | + => (IComplexProperty)base.Metadata; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Provides access to change tracking information and operations for a given property of this complex type. |
| 69 | + /// </summary> |
| 70 | + /// <remarks> |
| 71 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 72 | + /// examples. |
| 73 | + /// </remarks> |
| 74 | + /// <param name="property">The property to access information and operations for.</param> |
| 75 | + /// <returns>An object that exposes change tracking information and operations for the given property.</returns> |
| 76 | + public virtual PropertyEntry Property(IProperty property) |
| 77 | + { |
| 78 | + Check.NotNull(property, nameof(property)); |
| 79 | + |
| 80 | + return new PropertyEntry(InternalEntry, property); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Provides access to change tracking information and operations for a given property of this complex type. |
| 85 | + /// </summary> |
| 86 | + /// <remarks> |
| 87 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 88 | + /// examples. |
| 89 | + /// </remarks> |
| 90 | + /// <param name="propertyName">The property to access information and operations for.</param> |
| 91 | + /// <returns>An object that exposes change tracking information and operations for the given property.</returns> |
| 92 | + public virtual PropertyEntry Property(string propertyName) |
| 93 | + { |
| 94 | + Check.NotEmpty(propertyName, nameof(propertyName)); |
| 95 | + |
| 96 | + return new PropertyEntry(InternalEntry, Metadata.ComplexType.GetProperty(propertyName)); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Provides access to change tracking information and operations for all properties of this complex type. |
| 101 | + /// </summary> |
| 102 | + /// <remarks> |
| 103 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 104 | + /// examples. |
| 105 | + /// </remarks> |
| 106 | + public virtual IEnumerable<PropertyEntry> Properties |
| 107 | + => Metadata.ComplexType.GetProperties().Select(property => new PropertyEntry(InternalEntry, property)); |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Provides access to change tracking information and operations for a given property of a nested complex type on this |
| 111 | + /// complex type. |
| 112 | + /// </summary> |
| 113 | + /// <remarks> |
| 114 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 115 | + /// examples. |
| 116 | + /// </remarks> |
| 117 | + /// <param name="property">The property to access information and operations for.</param> |
| 118 | + /// <returns>An object that exposes change tracking information and operations for the given property.</returns> |
| 119 | + public virtual ComplexPropertyEntry ComplexProperty(IComplexProperty property) |
| 120 | + { |
| 121 | + Check.NotNull(property, nameof(property)); |
| 122 | + |
| 123 | + return new ComplexPropertyEntry(InternalEntry, property); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Provides access to change tracking information and operations for a given property of a nested complex type on this |
| 128 | + /// complex type. |
| 129 | + /// </summary> |
| 130 | + /// <remarks> |
| 131 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 132 | + /// examples. |
| 133 | + /// </remarks> |
| 134 | + /// <param name="propertyName">The property to access information and operations for.</param> |
| 135 | + /// <returns>An object that exposes change tracking information and operations for the given property.</returns> |
| 136 | + public virtual ComplexPropertyEntry ComplexProperty(string propertyName) |
| 137 | + { |
| 138 | + Check.NotEmpty(propertyName, nameof(propertyName)); |
| 139 | + |
| 140 | + return new ComplexPropertyEntry(InternalEntry, Metadata.ComplexType.GetComplexProperty(propertyName)); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Provides access to change tracking information and operations for all properties of nested complex types on this complex type. |
| 145 | + /// </summary> |
| 146 | + /// <remarks> |
| 147 | + /// See <see href="https://aka.ms/efcore-docs-entity-entries">Accessing tracked entities in EF Core</see> for more information and |
| 148 | + /// examples. |
| 149 | + /// </remarks> |
| 150 | + public virtual IEnumerable<ComplexPropertyEntry> ComplexProperties |
| 151 | + => Metadata.ComplexType.GetComplexProperties().Select(property => new ComplexPropertyEntry(InternalEntry, property)); |
| 152 | +} |
0 commit comments