Skip to content

Commit

Permalink
Implement DbSet.Find
Browse files Browse the repository at this point in the history
Issue #797

Semantics are essentially the same as EF6:
- If entity with given key is being tracked, then return it without a database query
- Otherwise, query the database and track and return entity if found
- Otherwise, return null
  • Loading branch information
ajcvickers committed Jun 16, 2016
1 parent 7130198 commit fa1621e
Show file tree
Hide file tree
Showing 21 changed files with 1,641 additions and 58 deletions.
713 changes: 713 additions & 0 deletions src/Microsoft.EntityFrameworkCore.Specification.Tests/FindTestBase.cs

Large diffs are not rendered by default.

Expand Up @@ -48,6 +48,7 @@
<Compile Include="DataAnnotationFixtureBase.cs" />
<Compile Include="DataAnnotationTestBase.cs" />
<Compile Include="F1FixtureBase.cs" />
<Compile Include="FindTestBase.cs" />
<Compile Include="GearsOfWarQueryFixtureBase.cs" />
<Compile Include="GearsOfWarQueryTestBase.cs" />
<Compile Include="GraphUpdatesTestBase.cs" />
Expand Down Expand Up @@ -210,4 +211,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Expand Up @@ -36,6 +36,13 @@ public CompositePrincipalKeyValueFactory([NotNull] IKey key)
: new CompositeComparer();
}

/// <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 object CreateFromKeyValues(object[] keyValues)
=> keyValues.Any(v => v == null) ? null : keyValues;

/// <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
Expand Up @@ -31,6 +31,12 @@ public interface IIdentityMap
/// </summary>
bool Contains([NotNull] IForeignKey foreignKey, ValueBuffer valueBuffer);

/// <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>
InternalEntityEntry TryGetEntry(object[] keyValues);

/// <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
Expand Up @@ -13,6 +13,12 @@ namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal
/// </summary>
public interface IPrincipalKeyValueFactory<TKey>
{
/// <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>
object CreateFromKeyValues(object[] keyValues);

/// <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
Expand Up @@ -35,6 +35,12 @@ public interface IStateManager
/// </summary>
void BeginTrackingQuery();

/// <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>
InternalEntityEntry TryGetEntry(IKey key, object[] keyValues);

/// <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
Expand Up @@ -77,6 +77,17 @@ public virtual bool Contains(IForeignKey foreignKey, ValueBuffer valueBuffer)
&& _identityMap.ContainsKey(key);
}

/// <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 TryGetEntry(object[] keyValues)
{
InternalEntityEntry entry;
var key = PrincipalKeyValueFactory.CreateFromKeyValues(keyValues);
return key != null && _identityMap.TryGetValue((TKey)key, out entry) ? entry : 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.
Expand Down
Expand Up @@ -31,6 +31,13 @@ public SimplePrincipalKeyValueFactory([NotNull] PropertyAccessors propertyAccess
: new NoNullsEqualityComparer();
}

/// <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 object CreateFromKeyValues(object[] keyValues)
=> keyValues[0];

/// <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

0 comments on commit fa1621e

Please sign in to comment.