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

Bring back AddAsync #6023

Merged
merged 1 commit into from
Jul 8, 2016
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 @@ -3,6 +3,8 @@

using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -56,6 +58,18 @@ protected override long GetNewLowValue()
typeof(long),
CultureInfo.InvariantCulture);

/// <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>
protected override async Task<long> GetNewLowValueAsync(CancellationToken cancellationToken = default(CancellationToken))
=> (long)Convert.ChangeType(
await _rawSqlCommandBuilder
.Build(_sqlGenerator.GenerateNextSequenceValueOperation(_sequence.Name, _sequence.Schema))
.ExecuteScalarAsync(_connection, cancellationToken: cancellationToken),
typeof(long),
CultureInfo.InvariantCulture);

/// <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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

Expand All @@ -17,5 +19,14 @@ public interface IKeyPropagator
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
void PropagateValue([NotNull] InternalEntityEntry entry, [NotNull] IProperty property);

/// <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>
Task PropagateValueAsync(
[NotNull] InternalEntityEntry entry,
[NotNull] IProperty property,
CancellationToken cancellationToken = default(CancellationToken));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

Expand All @@ -18,6 +20,14 @@ public interface IValueGenerationManager
/// </summary>
void Generate([NotNull] InternalEntityEntry entry);

/// <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>
Task GenerateAsync(
[NotNull] InternalEntityEntry entry,
CancellationToken cancellationToken = default(CancellationToken));

/// <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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -74,6 +76,25 @@ public virtual void SetEntityState(EntityState entityState, bool acceptChanges =
SetEntityState(oldState, entityState, acceptChanges);
}

/// <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 async Task SetEntityStateAsync(
EntityState entityState,
bool acceptChanges,
CancellationToken cancellationToken = default(CancellationToken))
{
var oldState = _stateData.EntityState;

if (PrepareForAdd(entityState))
{
await StateManager.ValueGeneration.GenerateAsync(this, cancellationToken);
}

SetEntityState(oldState, entityState, acceptChanges);
}

private bool PrepareForAdd(EntityState newState)
{
if (newState != EntityState.Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -48,6 +50,29 @@ public virtual void PropagateValue(InternalEntityEntry entry, IProperty property
}
}

/// <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 async Task PropagateValueAsync(
InternalEntityEntry entry,
IProperty property,
CancellationToken cancellationToken = default(CancellationToken))
{
Debug.Assert(property.IsForeignKey());

if (!TryPropagateValue(entry, property)
&& property.IsKey())
{
var valueGenerator = TryGetValueGenerator(property);

if (valueGenerator != null)
{
entry[property] = await valueGenerator.NextAsync(new EntityEntry(entry), cancellationToken);
}
}
}

private bool TryPropagateValue(InternalEntityEntry entry, IProperty property)
{
var entityType = entry.EntityType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// 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.Diagnostics;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand All @@ -10,7 +13,7 @@
namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// 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 class ValueGenerationManager : IValueGenerationManager
Expand All @@ -19,7 +22,7 @@ public class ValueGenerationManager : IValueGenerationManager
private readonly IKeyPropagator _keyPropagator;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// 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 ValueGenerationManager(
Expand All @@ -31,40 +34,86 @@ public ValueGenerationManager(
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// 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 void Generate(InternalEntityEntry entry)
{
var entityEntry = new EntityEntry(entry);

foreach (var propertyTuple in FindProperties(entry))
{
var property = propertyTuple.Item1;

if (propertyTuple.Item2)
{
_keyPropagator.PropagateValue(entry, property);
}
else
{
var valueGenerator = GetValueGenerator(entry, property);

SetGeneratedValue(
entry,
property,
valueGenerator.Next(entityEntry),
valueGenerator.GeneratesTemporaryValues);
}
}
}

/// <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 async Task GenerateAsync(
InternalEntityEntry entry,
CancellationToken cancellationToken = default(CancellationToken))
{
var entityEntry = new EntityEntry(entry);

foreach (var propertyTuple in FindProperties(entry))
{
var property = propertyTuple.Item1;

if (propertyTuple.Item2)
{
_keyPropagator.PropagateValue(entry, property);
}
else
{
var valueGenerator = GetValueGenerator(entry, property);

SetGeneratedValue(
entry,
property,
await valueGenerator.NextAsync(entityEntry, cancellationToken),
valueGenerator.GeneratesTemporaryValues);
}
}
}

private IEnumerable<Tuple<IProperty, bool>> FindProperties(InternalEntityEntry entry)
{
foreach (var property in entry.EntityType.GetProperties())
{
var isForeignKey = property.IsForeignKey();

if ((property.RequiresValueGenerator || isForeignKey)
&& property.ClrType.IsDefaultValue(entry[property]))
{
if (isForeignKey)
{
_keyPropagator.PropagateValue(entry, property);
}
else
{
var valueGenerator = _valueGeneratorSelector.Select(property, property.IsKey()
? property.DeclaringEntityType
: entry.EntityType);

Debug.Assert(valueGenerator != null);

SetGeneratedValue(entry, property, valueGenerator.Next(entityEntry), valueGenerator.GeneratesTemporaryValues);
}
yield return Tuple.Create(property, isForeignKey);
}
}
}

private ValueGenerator GetValueGenerator(InternalEntityEntry entry, IProperty property)
=> _valueGeneratorSelector.Select(property, property.IsKey()
? property.DeclaringEntityType
: entry.EntityType);

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// 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 bool MayGetTemporaryValue(IProperty property, IEntityType entityType)
Expand Down
Loading