/// <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 virtual IKeyValueIndex CreatePrincipalKeyValueFromOriginalValues(InternalEntityEntry entry, IForeignKey foreignKey) => new KeyValueIndex <TKey>( foreignKey, _principalKeyValueFactory.CreateFromOriginalValues(entry), _principalKeyValueFactory.EqualityComparer, fromOriginalValues: true);
public virtual IEnumerable<IAnnotation> For(IForeignKey foreignKey) => _empty;
public virtual IEnumerable<InternalEntityEntry> GetDependents(InternalEntityEntry principalEntry, IForeignKey foreignKey) { var keyValue = principalEntry.GetPrincipalKeyValue(foreignKey); Dictionary<EntityKey, HashSet<InternalEntityEntry>> fkMap; HashSet<InternalEntityEntry> dependents; return keyValue != EntityKey.InvalidEntityKey && _dependentsMap.TryGetValue(foreignKey, out fkMap) && fkMap.TryGetValue(keyValue, out dependents) ? dependents : Enumerable.Empty<InternalEntityEntry>(); }
public void DisplayForeignKeyProperties(IForeignKey foreignKey, TreeNode foreignKeyNode) { DataTable dt = new DataTable("MyData"); dt.Columns.Add("Property", stringType); dt.Columns.Add("Value", stringType); dt.Rows.Add(new object[] {"Name", foreignKey.Name}); dt.Rows.Add(new object[] {"Alias", foreignKey.Alias}); // NOTE, THIS MUST USE INDIRECTION THROUGH THE TABLE dt.Rows.Add(new object[] {"PrimaryTable", foreignKey.PrimaryTable.Name}); dt.Rows.Add(new object[] {"ForeignTable", foreignKey.ForeignTable.Name}); dt.Rows.Add(new object[] {"UpdateRule", foreignKey.UpdateRule}); dt.Rows.Add(new object[] {"DeleteRule", foreignKey.DeleteRule}); dt.Rows.Add(new object[] {"PrimaryKeyName", foreignKey.PrimaryKeyName}); dt.Rows.Add(new object[] {"Deferrability", foreignKey.Deferrability.ToString()}); this.Grid.DataSource = dt; this.InitializeGrid(); this.Text = "IForeignKey Properties"; this.lnkHELP.Text = "IForeignKey Help ..."; this.helpInterface = "IForeignKey"; }
public virtual InternalEntityEntry GetPrincipal(IPropertyAccessor dependentEntry, IForeignKey foreignKey) { var dependentKeyValue = dependentEntry.GetDependentKeyValue(foreignKey); if (dependentKeyValue == EntityKey.InvalidEntityKey) { return null; } var principalEntityType = foreignKey.PrincipalEntityType; var principalProperties = foreignKey.PrincipalKey.Properties; // TODO: Perf: Add additional indexes so that this isn't a linear lookup var principals = Entries.Where( e => e.EntityType == principalEntityType && dependentKeyValue.Equals( e.GetPrincipalKey(foreignKey, principalEntityType, principalProperties))).ToList(); if (principals.Count > 1) { // TODO: Better exception message // Issue #739 throw new InvalidOperationException("Multiple matching principals."); } return principals.FirstOrDefault(); }
private void StealReference(IForeignKey foreignKey, InternalEntityEntry dependentEntry) { foreach (var navigation in dependentEntry.EntityType.GetNavigations().Where(n => n.ForeignKey == foreignKey)) { if (navigation.PointsToPrincipal()) { _setterSource.GetAccessor(navigation).SetClrValue(dependentEntry.Entity, null); dependentEntry.RelationshipsSnapshot.TakeSnapshot(navigation); } } var nullableProperties = foreignKey.Properties.Where(p => p.IsNullable).ToList(); if (nullableProperties.Count > 0) { foreach (var property in nullableProperties) { dependentEntry[property] = null; } } }
internal static string GetAttribute(IForeignKey key, Type pkTableType) { return GetAttribute(key, pkTableType.FullName); }
public IEnumerable <IUpdateEntry> GetDependents(IUpdateEntry principalEntry, IForeignKey foreignKey) => throw new NotImplementedException();
public static ISqliteForeignKeyAnnotations Sqlite([NotNull] this IForeignKey foreignKey) => new ReadOnlySqliteForeignKeyAnnotations(foreignKey);
public static INavigation?GetNavigation([NotNull] this IForeignKey foreignKey, bool pointsToPrincipal) => pointsToPrincipal ? foreignKey.DependentToPrincipal : foreignKey.PrincipalToDependent;
public static string KeyName([NotNull] this IForeignKey foreignKey) { Check.NotNull(foreignKey, "foreignKey"); return(foreignKey[Annotations.KeyName]); }
public static IDependentKeyValueFactory <TKey>?GetDependentKeyValueFactory <TKey>( [NotNull] this IForeignKey foreignKey) => (IDependentKeyValueFactory <TKey>?)foreignKey.AsForeignKey().DependentKeyValueFactory;
protected virtual void CreateForeignKey(IForeignKey fk) { Put("^alter ^table %f ^add ", fk.Table); CreateForeignKeyCore(fk); EndCommand(); }
/// <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 virtual IKeyValueIndex CreateDependentKeyValueFromOriginalValues(InternalEntityEntry entry, IForeignKey foreignKey) { TKey keyValue; return(foreignKey.GetDependentKeyValueFactory <TKey>().TryCreateFromOriginalValues(entry, out keyValue) ? new KeyValueIndex <TKey>(foreignKey, keyValue, _principalKeyValueFactory.EqualityComparer, fromOriginalValues: true) : null); }
public virtual IRelationalForeignKeyExtensions Extensions(IForeignKey foreignKey) => foreignKey.Sqlite();
public virtual void Passes_for_compatible_duplicate_foreignKey_names_within_hierarchy_name_configured_explicitly() { var modelBuilder = CreateConventionalModelBuilder(); IForeignKey fk1 = null; IForeignKey fk2 = null; modelBuilder.Entity <Animal>(); modelBuilder.Entity <Cat>( et => { et.Property(c => c.Breed).HasColumnName("Breed"); fk1 = et .HasOne <Person>() .WithMany() .HasForeignKey( c => new { c.Name, c.Breed }) .HasPrincipalKey( p => new { p.Name, p.FavoriteBreed }) .HasConstraintName("FK") .Metadata; }); modelBuilder.Entity <Dog>( et => { et.Property(c => c.Breed).HasColumnName("Breed"); fk2 = et .HasOne <Customer>() .WithMany() .HasForeignKey( c => new { c.Name, c.Breed }) .HasPrincipalKey( p => new { p.Name, p.FavoriteBreed }) .HasConstraintName("FK") .Metadata; }); Validate(modelBuilder.Model); Assert.NotSame(fk1, fk2); Assert.Equal(fk1.GetConstraintName(), fk2.GetConstraintName()); var index1 = fk1.DeclaringEntityType.GetDeclaredIndexes().Single(); var index2 = fk2.DeclaringEntityType.GetDeclaredIndexes().Single(); Assert.NotSame(index1, index2); Assert.Equal(index1.GetName(), index2.GetName()); }
private void StealReference(IForeignKey foreignKey, StateEntry dependentEntry) { foreach (var navigation in dependentEntry.EntityType.Navigations.Where(n => n.ForeignKey == foreignKey)) { if (navigation.PointsToPrincipal) { _setterSource.GetAccessor(navigation).SetClrValue(dependentEntry.Entity, null); dependentEntry.RelationshipsSnapshot.TakeSnapshot(navigation); } } var nullableProperties = foreignKey.Properties.Where(p => p.IsNullable).ToArray(); if (nullableProperties.Length > 0) { foreach (var property in nullableProperties) { dependentEntry[property] = null; } } else { // TODO: Handle conceptual null } }
public void SetForeignKey(IForeignKey value) { }
private static void SetForeignKeyValue(IForeignKey foreignKey, InternalEntityEntry dependentEntry, IReadOnlyList<object> principalValues) { for (var i = 0; i < foreignKey.Properties.Count; i++) { if (foreignKey.Properties[i].GetGenerationProperty() == null || !foreignKey.PrincipalKey.Properties[i].IsSentinelValue(principalValues[i])) { var dependentProperty = foreignKey.Properties[i]; dependentEntry[dependentProperty] = principalValues[i]; dependentEntry.RelationshipsSnapshot.TakeSnapshot(dependentProperty); } } }
private void DoFixup(IForeignKey foreignKey, StateEntry principalEntry, StateEntry[] dependentEntries) { DoFixup(_configuration.StateManager.Model.GetNavigations(foreignKey).ToArray(), principalEntry, dependentEntries); }
public ForeignKeys(IForeignKey[] columns) { this.keys = columns; }
private void GenerateRelationship(IForeignKey foreignKey, bool useDataAnnotations) { var canUseDataAnnotations = true; var annotations = foreignKey.GetAnnotations().ToList(); var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasOne)}(" + (foreignKey.DependentToPrincipal != null ? $"d => d.{foreignKey.DependentToPrincipal.Name}" : null) + ")", $".{(foreignKey.IsUnique ? nameof(ReferenceNavigationBuilder.WithOne) : nameof(ReferenceNavigationBuilder.WithMany))}" + $"(" + (foreignKey.PrincipalToDependent != null ? $"p => p.{foreignKey.PrincipalToDependent.Name}" : null) + ")" }; if (!foreignKey.PrincipalKey.IsPrimaryKey()) { canUseDataAnnotations = false; lines.Add( $".{nameof(ReferenceReferenceBuilder.HasPrincipalKey)}" + (foreignKey.IsUnique ? $"<{((ITypeBase)foreignKey.PrincipalEntityType).DisplayName()}>" : "") + $"(p => {GenerateLambdaToKey(foreignKey.PrincipalKey.Properties, "p")})"); } lines.Add( $".{nameof(ReferenceReferenceBuilder.HasForeignKey)}" + (foreignKey.IsUnique ? $"<{((ITypeBase)foreignKey.DeclaringEntityType).DisplayName()}>" : "") + $"(d => {GenerateLambdaToKey(foreignKey.Properties, "d")})"); var defaultOnDeleteAction = foreignKey.IsRequired ? DeleteBehavior.Cascade : DeleteBehavior.ClientSetNull; if (foreignKey.DeleteBehavior != defaultOnDeleteAction) { canUseDataAnnotations = false; lines.Add( $".{nameof(ReferenceReferenceBuilder.OnDelete)}" + $"({_code.Literal(foreignKey.DeleteBehavior)})"); } if (!string.IsNullOrEmpty((string)foreignKey[RelationalAnnotationNames.Name])) { canUseDataAnnotations = false; lines.Add( $".{nameof(RelationalReferenceReferenceBuilderExtensions.HasConstraintName)}" + $"({_code.Literal(foreignKey.Relational().Name)})"); RemoveAnnotation(ref annotations, RelationalAnnotationNames.Name); } var annotationsToRemove = new List <IAnnotation>(); foreach (var annotation in annotations) { if (_annotationCodeGenerator.IsHandledByConvention(foreignKey, annotation)) { annotationsToRemove.Add(annotation); } else { var methodCall = _annotationCodeGenerator.GenerateFluentApi(foreignKey, annotation); if (methodCall != null) { canUseDataAnnotations = false; lines.Add(_code.Fragment(methodCall)); annotationsToRemove.Add(annotation); } } } lines.AddRange(GenerateAnnotations(annotations.Except(annotationsToRemove))); if (!useDataAnnotations || !canUseDataAnnotations) { AppendMultiLineFluentApi(foreignKey.DeclaringEntityType, lines); } }
public IRelationalForeignKeyAnnotations For(IForeignKey foreignKey) => foreignKey.TestProvider();
public RelationalForeignKeyAnnotations([NotNull] IForeignKey foreignKey, [CanBeNull] RelationalFullAnnotationNames providerFullAnnotationNames) : this(new RelationalAnnotations(foreignKey), providerFullAnnotationNames) { }
public virtual IRelationalForeignKeyAnnotations For(IForeignKey foreignKey) => foreignKey.MyCat();
/// <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 virtual InternalEntityEntry GetPrincipal(InternalEntityEntry dependentEntry, IForeignKey foreignKey) => FindIdentityMap(foreignKey.PrincipalKey)?.TryGetEntry(foreignKey, dependentEntry);
public virtual InternalEntityEntry GetPrincipal(IPropertyAccessor dependentEntry, IForeignKey foreignKey) { var dependentKeyValue = dependentEntry.GetDependentKeyValue(foreignKey); if (dependentKeyValue == EntityKey.InvalidEntityKey) { return null; } InternalEntityEntry principalEntry; _identityMap.TryGetValue(dependentKeyValue, out principalEntry); return principalEntry; }
/// <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 virtual InternalEntityEntry GetPrincipalUsingPreStoreGeneratedValues(InternalEntityEntry dependentEntry, IForeignKey foreignKey) => FindIdentityMap(foreignKey.PrincipalKey)?.TryGetEntryUsingPreStoreGeneratedValues(foreignKey, dependentEntry);
public IEnumerable<InternalEntityEntry> GetDependents(InternalEntityEntry principalEntry, IForeignKey foreignKey) { throw new NotImplementedException(); }
/// <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 virtual InternalEntityEntry GetPrincipalUsingRelationshipSnapshot(InternalEntityEntry dependentEntry, IForeignKey foreignKey) => FindIdentityMap(foreignKey.PrincipalKey)?.TryGetEntryUsingRelationshipSnapshot(foreignKey, dependentEntry);
/// <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 virtual IEnumerable<InternalEntityEntry> GetDependentsFromNavigation(InternalEntityEntry principalEntry, IForeignKey foreignKey) { var navigation = foreignKey.PrincipalToDependent; if (navigation == null) { return null; } var navigationValue = principalEntry[navigation]; if (navigationValue == null) { return Enumerable.Empty<InternalEntityEntry>(); } if (foreignKey.IsUnique) { var dependentEntry = TryGetEntry(navigationValue); return dependentEntry != null ? new[] { dependentEntry } : Enumerable.Empty<InternalEntityEntry>(); } return ((IEnumerable<object>)navigationValue).Select(TryGetEntry).Where(e => e != null); }
/// <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 virtual IEnumerable <InternalEntityEntry> GetDependentsFromNavigation(InternalEntityEntry principalEntry, IForeignKey foreignKey) { var navigation = foreignKey.PrincipalToDependent; if (navigation == null) { return(null); } var navigationValue = principalEntry[navigation]; if (navigationValue == null) { return(Enumerable.Empty <InternalEntityEntry>()); } if (foreignKey.IsUnique) { var dependentEntry = TryGetEntry(navigationValue); return(dependentEntry != null ? new[] { dependentEntry } : Enumerable.Empty <InternalEntityEntry>()); } return(((IEnumerable <object>)navigationValue).Select(v => TryGetEntry(v)).Where(e => e != null)); }
private void Format(IForeignKey foreignKey, IReadOnlyModificationCommand source, IReadOnlyModificationCommand target, StringBuilder builder) { var reverseDependency = !source.Entries.Any(e => foreignKey.DeclaringEntityType.IsAssignableFrom(e.EntityType)); if (reverseDependency) { builder.AppendLine(" <-"); } else { builder.Append(' '); } if (foreignKey.DependentToPrincipal != null || foreignKey.PrincipalToDependent != null) { if (!reverseDependency && foreignKey.DependentToPrincipal != null) { builder.Append(foreignKey.DependentToPrincipal.Name); builder.Append(' '); } if (foreignKey.PrincipalToDependent != null) { builder.Append(foreignKey.PrincipalToDependent.Name); builder.Append(' '); } if (reverseDependency && foreignKey.DependentToPrincipal != null) { builder.Append(foreignKey.DependentToPrincipal.Name); builder.Append(' '); } } else { builder.Append("ForeignKey "); } var dependentCommand = reverseDependency ? target : source; var dependentEntry = dependentCommand.Entries.First(e => foreignKey.DeclaringEntityType.IsAssignableFrom(e.EntityType)); builder.Append("{ "); for (var i = 0; i < foreignKey.Properties.Count; i++) { var property = foreignKey.Properties[i]; builder.Append('\''); builder.Append(property.Name); builder.Append('\''); if (_sensitiveLoggingEnabled) { builder.Append(": "); builder.Append(dependentEntry.GetCurrentValue(property)); } if (i != foreignKey.Properties.Count - 1) { builder.Append(", "); } } builder.Append(" } "); if (!reverseDependency) { builder.AppendLine("<-"); } }
private void DoFixup(IForeignKey foreignKey, InternalEntityEntry principalEntry, InternalEntityEntry[] dependentEntries) => DoFixup(_model.GetNavigations(foreignKey).ToList(), principalEntry, dependentEntries);
protected virtual IEnumerable <MigrationOperation> Diff(IForeignKey source, IForeignKey target, DiffContext diffContext) => HasDifferences(MigrationsAnnotations.For(source), MigrationsAnnotations.For(target)) ? Remove(source, diffContext).Concat(Add(target, diffContext)) : Enumerable.Empty <MigrationOperation>();
private static void SetForeignKeyValue( IForeignKey foreignKey, InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry) => SetForeignKeyValue(foreignKey, dependentEntry, foreignKey.PrincipalKey.Properties.Select(p => principalEntry[p]).ToList());
public static IEnumerable <INavigation> GetNavigations( [NotNull] this IModel model, [NotNull] IForeignKey foreignKey) { // TODO: Perf: consider not needing to do a full scan here return(model.EntityTypes.SelectMany(e => e.Navigations).Where(n => n.ForeignKey == foreignKey)); }
private static void AddMatchingPredecessorEdge( Dictionary<IKeyValueIndex, List<ModificationCommand>> predecessorsMap, IKeyValueIndex dependentKeyValue, Multigraph<ModificationCommand, IForeignKey> commandGraph, ModificationCommand command, IForeignKey foreignKey) { List<ModificationCommand> predecessorCommands; if (predecessorsMap.TryGetValue(dependentKeyValue, out predecessorCommands)) { foreach (var predecessor in predecessorCommands) { if (predecessor != command) { commandGraph.AddEdge(predecessor, command, foreignKey); } } } }
/// <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 bool AreCompatible( [NotNull] this IForeignKey foreignKey, [NotNull] IForeignKey duplicateForeignKey, [NotNull] string tableName, [CanBeNull] string schema, bool shouldThrow) { var principalType = foreignKey.PrincipalEntityType; var duplicatePrincipalType = duplicateForeignKey.PrincipalEntityType; if (!string.Equals(principalType.GetSchema(), duplicatePrincipalType.GetSchema(), StringComparison.OrdinalIgnoreCase) || !string.Equals(principalType.GetTableName(), duplicatePrincipalType.GetTableName(), StringComparison.OrdinalIgnoreCase)) { if (shouldThrow) { throw new InvalidOperationException( RelationalStrings.DuplicateForeignKeyPrincipalTableMismatch( foreignKey.Properties.Format(), foreignKey.DeclaringEntityType.DisplayName(), duplicateForeignKey.Properties.Format(), duplicateForeignKey.DeclaringEntityType.DisplayName(), foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(), foreignKey.GetConstraintName(tableName, schema, foreignKey.PrincipalEntityType.GetTableName(), foreignKey.PrincipalEntityType.GetSchema()), principalType.GetSchemaQualifiedTableName(), duplicatePrincipalType.GetSchemaQualifiedTableName())); } return(false); } if (!foreignKey.Properties.Select(p => p.GetColumnName(tableName, schema)) .SequenceEqual(duplicateForeignKey.Properties.Select(p => p.GetColumnName(tableName, schema)))) { if (shouldThrow) { throw new InvalidOperationException( RelationalStrings.DuplicateForeignKeyColumnMismatch( foreignKey.Properties.Format(), foreignKey.DeclaringEntityType.DisplayName(), duplicateForeignKey.Properties.Format(), duplicateForeignKey.DeclaringEntityType.DisplayName(), foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(), foreignKey.GetConstraintName(tableName, schema, foreignKey.PrincipalEntityType.GetTableName(), foreignKey.PrincipalEntityType.GetSchema()), foreignKey.Properties.FormatColumns(tableName, schema), duplicateForeignKey.Properties.FormatColumns(tableName, schema))); } return(false); } if (!foreignKey.PrincipalKey.Properties .Select(p => p.GetColumnName(tableName, schema)) .SequenceEqual( duplicateForeignKey.PrincipalKey.Properties .Select(p => p.GetColumnName(tableName, schema)))) { if (shouldThrow) { throw new InvalidOperationException( RelationalStrings.DuplicateForeignKeyPrincipalColumnMismatch( foreignKey.Properties.Format(), foreignKey.DeclaringEntityType.DisplayName(), duplicateForeignKey.Properties.Format(), duplicateForeignKey.DeclaringEntityType.DisplayName(), foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(), foreignKey.GetConstraintName(tableName, schema, foreignKey.PrincipalEntityType.GetTableName(), foreignKey.PrincipalEntityType.GetSchema()), foreignKey.PrincipalKey.Properties.FormatColumns(tableName, schema), duplicateForeignKey.PrincipalKey.Properties.FormatColumns(tableName, schema))); } return(false); } if (foreignKey.IsUnique != duplicateForeignKey.IsUnique) { if (shouldThrow) { throw new InvalidOperationException( RelationalStrings.DuplicateForeignKeyUniquenessMismatch( foreignKey.Properties.Format(), foreignKey.DeclaringEntityType.DisplayName(), duplicateForeignKey.Properties.Format(), duplicateForeignKey.DeclaringEntityType.DisplayName(), foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(), foreignKey.GetConstraintName(tableName, schema, foreignKey.PrincipalEntityType.GetTableName(), foreignKey.PrincipalEntityType.GetSchema()))); } return(false); } if (foreignKey.DeleteBehavior != duplicateForeignKey.DeleteBehavior) { if (shouldThrow) { throw new InvalidOperationException( RelationalStrings.DuplicateForeignKeyDeleteBehaviorMismatch( foreignKey.Properties.Format(), foreignKey.DeclaringEntityType.DisplayName(), duplicateForeignKey.Properties.Format(), duplicateForeignKey.DeclaringEntityType.DisplayName(), foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(), foreignKey.GetConstraintName(tableName, schema, foreignKey.PrincipalEntityType.GetTableName(), foreignKey.PrincipalEntityType.GetSchema()), foreignKey.DeleteBehavior, duplicateForeignKey.DeleteBehavior)); } return(false); } return(true); }
internal static string GetAttribute(IForeignKey key, string dpoClassFullName) { return string.Format("[{0}(typeof({1}), {1}._{2})]", typeof(ForeignKeyAttribute).Name.Replace("Attribute", ""), dpoClassFullName, key.PK_Column); }
private bool Contains(IForeignKey inheritedFk, IForeignKey derivedFk) => inheritedFk != null && inheritedFk.PrincipalEntityType.IsAssignableFrom(derivedFk.PrincipalEntityType) && PropertyListComparer.Instance.Equals(inheritedFk.Properties, derivedFk.Properties);
public ReadOnlyRelationalForeignKeyExtensions([NotNull] IForeignKey foreignKey) { Check.NotNull(foreignKey, nameof(foreignKey)); _foreignKey = foreignKey; }
/// <summary> /// Generates code for an <see cref="IForeignKey" />. /// </summary> /// <param name="builderName"> The name of the builder variable. </param> /// <param name="foreignKey"> The foreign key. </param> /// <param name="stringBuilder"> The builder code is added to. </param> protected virtual void GenerateForeignKey( [NotNull] string builderName, [NotNull] IForeignKey foreignKey, [NotNull] IndentedStringBuilder stringBuilder) { Check.NotNull(builderName, nameof(builderName)); Check.NotNull(foreignKey, nameof(foreignKey)); Check.NotNull(stringBuilder, nameof(stringBuilder)); stringBuilder .Append(builderName) .Append(".HasOne(") .Append(Code.Literal(foreignKey.PrincipalEntityType.Name)); if (foreignKey.DependentToPrincipal != null) { stringBuilder .Append(", ") .Append(Code.Literal(foreignKey.DependentToPrincipal.Name)); } stringBuilder .Append(")") .AppendLine(); using (stringBuilder.Indent()) { if (foreignKey.IsUnique) { stringBuilder .Append(".WithOne("); if (foreignKey.PrincipalToDependent != null) { stringBuilder .Append(Code.Literal(foreignKey.PrincipalToDependent.Name)); } stringBuilder .AppendLine(")") .Append(".HasForeignKey(") .Append(Code.Literal(foreignKey.DeclaringEntityType.Name)) .Append(", ") .Append(string.Join(", ", foreignKey.Properties.Select(p => Code.Literal(p.Name)))) .Append(")"); GenerateForeignKeyAnnotations(foreignKey, stringBuilder); if (foreignKey.PrincipalKey != foreignKey.PrincipalEntityType.FindPrimaryKey()) { stringBuilder .AppendLine() .Append(".HasPrincipalKey(") .Append(Code.Literal(foreignKey.PrincipalEntityType.Name)) .Append(", ") .Append(string.Join(", ", foreignKey.PrincipalKey.Properties.Select(p => Code.Literal(p.Name)))) .Append(")"); } } else { stringBuilder .Append(".WithMany("); if (foreignKey.PrincipalToDependent != null) { stringBuilder .Append(Code.Literal(foreignKey.PrincipalToDependent.Name)); } stringBuilder .AppendLine(")") .Append(".HasForeignKey(") .Append(string.Join(", ", foreignKey.Properties.Select(p => Code.Literal(p.Name)))) .Append(")"); GenerateForeignKeyAnnotations(foreignKey, stringBuilder); if (foreignKey.PrincipalKey != foreignKey.PrincipalEntityType.FindPrimaryKey()) { stringBuilder .AppendLine() .Append(".HasPrincipalKey(") .Append(string.Join(", ", foreignKey.PrincipalKey.Properties.Select(p => Code.Literal(p.Name)))) .Append(")"); } } if (foreignKey.DeleteBehavior != DeleteBehavior.ClientSetNull) { stringBuilder .AppendLine() .Append(".OnDelete(") .Append(Code.Literal(foreignKey.DeleteBehavior)) .Append(")"); } } stringBuilder.AppendLine(";"); }
public virtual IEnumerable<IAnnotation> For(IForeignKey foreignKey) => Enumerable.Empty<IAnnotation>();
public InternalEntityEntry GetPrincipal(InternalEntityEntry entityEntry, IForeignKey foreignKey) => throw new NotImplementedException();
public IRelationalForeignKeyExtensions Extensions(IForeignKey foreignKey) => foreignKey.Relational();
public InternalEntityEntry GetPrincipalUsingPreStoreGeneratedValues(InternalEntityEntry entityEntry, IForeignKey foreignKey) => throw new NotImplementedException();
public virtual IEnumerable<InternalEntityEntry> GetDependents(InternalEntityEntry principalEntry, IForeignKey foreignKey) { var principalKeyValue = principalEntry.GetPrincipalKeyValue(foreignKey); // TODO: Perf: Add additional indexes so that this isn't a linear lookup return principalKeyValue == EntityKey.InvalidEntityKey ? Enumerable.Empty<InternalEntityEntry>() : Entries.Where( e => e.EntityType == foreignKey.EntityType && principalKeyValue.Equals(e.GetDependentKeyValue(foreignKey))); }
public InternalEntityEntry GetPrincipalUsingRelationshipSnapshot(InternalEntityEntry entityEntry, IForeignKey foreignKey) => throw new NotImplementedException();
public virtual IRelationalForeignKeyAnnotations For(IForeignKey foreignKey) => foreignKey.Sqlite();
public void UpdateDependentMap(InternalEntityEntry entry, IForeignKey foreignKey) => throw new NotImplementedException();
private ForeignKey BuildForeignKey(DatabaseModel database, IForeignKey foreignKey) { Check.NotNull(foreignKey, "foreignKey"); var table = database.GetTable(GetSchemaQualifiedName(foreignKey.EntityType)); var referencedTable = database.GetTable(GetSchemaQualifiedName(foreignKey.ReferencedEntityType)); var columns = foreignKey.Properties.Select( p => table.GetColumn(p.ColumnName())).ToArray(); var referenceColumns = foreignKey.ReferencedProperties.Select( p => referencedTable.GetColumn(p.ColumnName())).ToArray(); var cascadeDelete = foreignKey.CascadeDelete(); var storeForeignKey = new ForeignKey( ForeignKeyName(foreignKey), columns, referenceColumns, cascadeDelete); table.AddForeignKey(storeForeignKey); return storeForeignKey; }
public IEnumerable <InternalEntityEntry> GetDependentsUsingRelationshipSnapshot( InternalEntityEntry principalEntry, IForeignKey foreignKey) => throw new NotImplementedException();
public virtual void UpdateDependentMap(InternalEntityEntry entry, EntityKey oldKey, IForeignKey foreignKey) { if (entry.EntityState == EntityState.Detached) { return; } var newKey = entry.GetDependentKeyValue(foreignKey); if (oldKey.Equals(newKey)) { return; } Dictionary<EntityKey, HashSet<InternalEntityEntry>> fkMap; if (_dependentsMap.TryGetValue(foreignKey, out fkMap)) { HashSet<InternalEntityEntry> dependents; if (oldKey != EntityKey.InvalidEntityKey && fkMap.TryGetValue(oldKey, out dependents)) { dependents.Remove(entry); if (dependents.Count == 0) { fkMap.Remove(oldKey); } } if (newKey == EntityKey.InvalidEntityKey) { if (fkMap.Count == 0) { _dependentsMap.Remove(foreignKey); } } else { if (!fkMap.TryGetValue(newKey, out dependents)) { dependents = new HashSet<InternalEntityEntry>(); fkMap[newKey] = dependents; } dependents.Add(entry); } } }
public IEnumerable <InternalEntityEntry> GetDependentsFromNavigation(InternalEntityEntry principalEntry, IForeignKey foreignKey) => throw new NotImplementedException();
public void UpdateDependentMap(InternalEntityEntry entry, IKeyValue oldKeyValue, IForeignKey foreignKey) { throw new NotImplementedException(); }
/// <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 virtual IEnumerable<InternalEntityEntry> GetDependentsUsingRelationshipSnapshot( InternalEntityEntry principalEntry, IForeignKey foreignKey) { var dependentIdentityMap = FindIdentityMap(foreignKey.DeclaringEntityType.FindPrimaryKey()); return dependentIdentityMap != null ? dependentIdentityMap.GetDependentsMap(foreignKey).GetDependentsUsingRelationshipSnapshot(principalEntry) : Enumerable.Empty<InternalEntityEntry>(); }
public InternalEntityEntry GetPrincipal(IPropertyAccessor dependentEntry, IForeignKey foreignKey) { throw new NotImplementedException(); }
public static IRelationalForeignKeyAnnotations Sqlite([NotNull] this IForeignKey foreignKey) => new RelationalForeignKeyAnnotations(Check.NotNull(foreignKey, nameof(foreignKey)), SqliteAnnotationNames.Prefix);