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

Added TryGetValue<T> for Properties - fix #21343 #21911

Merged
merged 6 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions src/EFCore/ChangeTracking/PropertyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ public virtual IEntityType EntityType
/// <returns> The value of the property. </returns>
public abstract TValue GetValue<TValue>([NotNull] string propertyName);

/// <summary>
/// Safely gets the value of the property just like using the indexed property getter but
/// typed to the type of the generic parameter.
/// If property exists it return into the out parameter, otherwise the default value of TValue
/// </summary>
/// <typeparam name="TValue"> The type of the property. </typeparam>
/// <param name="propertyName"> The property name. </param>
/// <param name="value"> The property value if any. </param>
/// <returns> True if the property exists, otherwise false. </returns>
public bool TryGetValue<TValue>([NotNull] string propertyName, out TValue value)
{
var property = EntityType.FindProperty(propertyName);
if (propertyName != null)
{
value = this.GetValue<TValue>(propertyName);
m4ss1m0g marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

value = default(TValue);
return false;
}

/// <summary>
/// Gets the value of the property just like using the indexed property getter but
/// typed to the type of the generic parameter.
Expand Down
94 changes: 94 additions & 0 deletions test/EFCore.Tests/ChangeTracking/Internal/PropertyValuesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
{
public class PropertyValuesTest
{
[ConditionalFact]
public void Can_safely_get_originalvalue_and_currentvalue_with_tryget()
{
// Arrange
const string NameValue = "Simple Name";
const string NewNameValue = "A New Name";

using var ctx = new CurrentValuesDb();
var entity = ctx.SimpleEntities.Add(new SimpleEntity { Name = NameValue });
ctx.SaveChanges();

entity.Entity.Name = NewNameValue;

// Act
var current = entity.CurrentValues.TryGetValue<string>("Name", out var currentName);
var original = entity.OriginalValues.TryGetValue<string>("Name", out var originalName);

// Assert
Assert.True(current);
Assert.True(original);

Assert.Equal(NameValue, originalName);
Assert.Equal(NewNameValue, currentName);
}

[ConditionalFact]
public void Should_not_throw_error_when_property_do_not_exist()
{
// Arrange
const string NameValue = "Simple Name";
const string NewNameValue = "A New Name";

using var ctx = new CurrentValuesDb();
var entity = ctx.SimpleEntities.Add(new SimpleEntity { Name = NameValue });
ctx.SaveChanges();

entity.Entity.Name = NewNameValue;

// Act
var current = entity.CurrentValues.TryGetValue<string>("Non_Existent_Property", out var non_existent_current);
var original = entity.OriginalValues.TryGetValue<string>("Non_Existent_Property", out var non_existent_original);

// Assert
Assert.False(current);
Assert.False(original);

Assert.Null(non_existent_current);
Assert.Null(non_existent_original);
}

private class CurrentValuesDb : DbContext
{
public DbSet<SimpleEntity> SimpleEntities { get; set; }

protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseInMemoryDatabase("DB1");
}
}

private class SimpleEntity
{
public int Id { get; set; }

public string Name { get; set; }

public IEnumerable<RelatedEntity> RelatedEntities { get; set; }

}

private class RelatedEntity
{
public int Id { get; set; }

public int? SimpleEntityId { get; set; }

public SimpleEntity SimpleEntity { get; set; }

public string Name {get; set;}
}
}
}