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

Propagate the temporality of values. #7285

Merged
merged 1 commit into from
Jan 9, 2017
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 @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;

namespace Microsoft.EntityFrameworkCore.Update.Internal
Expand Down Expand Up @@ -60,6 +61,8 @@ public virtual IEnumerable<ModificationCommandBatch> BatchCommands(IReadOnlyList
var batch = _modificationCommandBatchFactory.Create();
foreach (var modificationCommand in independentCommandSet)
{
Validate(modificationCommand);

if (!batch.AddCommand(modificationCommand))
{
yield return batch;
Expand Down Expand Up @@ -285,5 +288,21 @@ private static void AddMatchingPredecessorEdge(
}
}
}

private void Validate(ModificationCommand modificationCommand)
{
if (modificationCommand.EntityState == EntityState.Added)
{
foreach (var columnModification in modificationCommand.ColumnModifications)
{
if (!columnModification.IsRead
&& columnModification.Entry.HasTemporaryValue(columnModification.Property))
{
throw new InvalidOperationException(
CoreStrings.TempValue(columnModification.Property.Name, columnModification.Entry.EntityType.DisplayName()));
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ private IReadOnlyList<ColumnModification> GenerateColumnModifications()
var readValue = entry.IsStoreGenerated(property);
var writeValue = !readValue && (adding || entry.IsModified(property));

if (adding
&& !readValue
&& entry.HasTemporaryValue(property))
{
throw new InvalidOperationException(CoreStrings.TempValue(property.Name, entityType.DisplayName()));
}

if (readValue
|| writeValue
|| isCondition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,13 @@ public virtual bool IsConceptualNull([NotNull] IProperty property)
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual bool HasTemporaryValue(IProperty property)
=> (_stateData.EntityState == EntityState.Added || _stateData.EntityState == EntityState.Detached)
&& _stateData.IsPropertyFlagged(property.GetIndex(), PropertyFlag.TemporaryOrModified);
{
object _;
return (_stateData.EntityState == EntityState.Added || _stateData.EntityState == EntityState.Detached)
&& _stateData.IsPropertyFlagged(property.GetIndex(), PropertyFlag.TemporaryOrModified)
&& (_storeGeneratedValues.IsEmpty
|| !_storeGeneratedValues.TryGetValue(property, out _));
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public virtual void PropagateValue(InternalEntityEntry entry, IProperty property
if (valueGenerator != null)
{
entry[property] = valueGenerator.Next(new EntityEntry(entry));

if (valueGenerator.GeneratesTemporaryValues)
{
entry.MarkAsTemporary(property);
}
}
}
}
Expand All @@ -69,6 +74,11 @@ public virtual async Task PropagateValueAsync(
if (valueGenerator != null)
{
entry[property] = await valueGenerator.NextAsync(new EntityEntry(entry), cancellationToken);

if (valueGenerator.GeneratesTemporaryValues)
{
entry.MarkAsTemporary(property);
}
}
}
}
Expand Down Expand Up @@ -109,6 +119,16 @@ private bool TryPropagateValue(InternalEntityEntry entry, IProperty property)
if (!principalProperty.ClrType.IsDefaultValue(principalValue))
{
entry[property] = principalValue;

if (principalEntry.HasTemporaryValue(principalProperty))
{
entry.MarkAsTemporary(property);
}
else
{
entry.MarkAsTemporary(property, isTemporary: false);
}

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ public static class MutableEntityTypeExtensions
/// </summary>
public static IEnumerable<IMutableForeignKey> GetDeclaredForeignKeys([NotNull] this IMutableEntityType entityType)
=> ((IEntityType)entityType).GetDeclaredForeignKeys().Cast<IMutableForeignKey>();

/// <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 static IEnumerable<IMutableProperty> GetDeclaredProperties([NotNull] this IMutableEntityType entityType)
=> ((IEntityType)entityType).GetDeclaredProperties().Cast<IMutableProperty>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ public void BatchCommands_creates_batches_lazily()
Times.Exactly(2));
}

[Fact]
public void BatchCommands_throws_on_non_store_generated_temporary_values()
{
var configuration = CreateContextServices(CreateTwoLevelFKModel());
var stateManager = configuration.GetRequiredService<IStateManager>();

var entry = stateManager.GetOrCreateEntry(new FakeEntity { Id = 1, Value = "Test" });
entry.SetEntityState(EntityState.Added);
entry.MarkAsTemporary(entry.EntityType.FindProperty(nameof(FakeEntity.Value)));

Assert.Equal(
CoreStrings.TempValue(nameof(FakeEntity.Value), nameof(FakeEntity)),
Assert.Throws<InvalidOperationException>(() => CreateCommandBatchPreparer().BatchCommands(new[] { entry }).ToList()).Message);
}

[Fact]
public void Batch_command_throws_on_commands_with_circular_dependencies()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,6 @@ public void RequiresResultPropagation_false_for_Update_operation_if_no_non_key_s
Assert.False(command.RequiresResultPropagation);
}

[Fact]
public void ColumnModifications_throw_on_temporary_values_with_no_store_generation_configured()
{
var entry = CreateEntry(EntityState.Added);
entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0]);

var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
command.AddEntry(entry);

Assert.Equal(
CoreStrings.TempValue("Id", "T1"),
Assert.Throws<InvalidOperationException>(() => command.ColumnModifications).Message);
}

private class T1
{
public int Id { get; set; }
Expand Down
Loading