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

Make discriminator read-only by default #21796

Merged
merged 4 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4112,7 +4112,8 @@ private InternalPropertyBuilder GetOrCreateDiscriminatorProperty(Type type, stri
type ?? discriminatorProperty?.ClrType ?? _defaultDiscriminatorType,
name ?? discriminatorProperty?.Name ?? _defaultDiscriminatorName,
typeConfigurationSource: type != null ? configurationSource : (ConfigurationSource?)null,
configurationSource: configurationSource);
configurationSource: configurationSource)
ajcvickers marked this conversation as resolved.
Show resolved Hide resolved
?.AfterSave(PropertySaveBehavior.Throw, ConfigurationSource.Convention);
}

private DiscriminatorBuilder DiscriminatorBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
Expand Down Expand Up @@ -373,6 +374,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
modelBuilder.Entity<Produce>()
.HasIndex(e => e.BarCode)
.IsUnique();

modelBuilder.Entity<OptionalSingle2Derived>()
.Property<string>("Discriminator")
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save);
}

protected virtual object CreateFullGraph()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Xunit;

// ReSharper disable AccessToDisposedClosure
Expand All @@ -16,6 +17,54 @@ namespace Microsoft.EntityFrameworkCore
public abstract partial class GraphUpdatesTestBase<TFixture>
where TFixture : GraphUpdatesTestBase<TFixture>.GraphUpdatesFixtureBase, new()
{
[ConditionalFact]
public virtual void Mutating_discriminator_value_throws_by_convention()
{
ExecuteWithStrategyInTransaction(
context =>
{
var instance = context.Set<OptionalSingle1Derived>().First();

var propertyEntry = context.Entry(instance).Property("Discriminator");

Assert.Equal(nameof(OptionalSingle1Derived), propertyEntry.CurrentValue);

propertyEntry.CurrentValue = nameof(OptionalSingle1MoreDerived);

Assert.Equal(
CoreStrings.PropertyReadOnlyAfterSave("Discriminator", nameof(OptionalSingle1Derived)),
Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).Message);
});
}

[ConditionalFact]
public virtual void Mutating_discriminator_value_can_be_configured_to_allow_mutation()
{
var id = 0;
ExecuteWithStrategyInTransaction(
context =>
{
var instance = context.Set<OptionalSingle2Derived>().First();
var propertyEntry = context.Entry(instance).Property("Discriminator");
id = instance.Id;

Assert.IsType<OptionalSingle2Derived>(instance);
Assert.Equal(nameof(OptionalSingle2Derived), propertyEntry.CurrentValue);

propertyEntry.CurrentValue = nameof(OptionalSingle2);

context.SaveChanges();
},
context =>
{
var instance = context.Set<OptionalSingle2>().First(e => e.Id == id);
var propertyEntry = context.Entry(instance).Property("Discriminator");

Assert.IsType<OptionalSingle2>(instance);
Assert.Equal(nameof(OptionalSingle2), propertyEntry.CurrentValue);
});
}

[ConditionalTheory]
[InlineData((int)ChangeMechanism.Fk)]
[InlineData((int)ChangeMechanism.Dependent)]
Expand Down