@@ -34,43 +34,79 @@ public virtual TEntity Create()
3434 var entity = Activator . CreateInstance < TEntity > ( ) ;
3535 Add ( entity ) ;
3636 return entity ;
37- }
38-
39- /// <summary>
40- /// Finds an entity with the given primary key value. If an entity with the given primary key value
41- /// is being tracked by the context, then it is returned immediately without making a request to the
42- /// database. Otherwise, a query is made to the database for an entity with the given primary key value
43- /// and this entity, if found, is attached to the context and returned. If no entity is found, then
44- /// null is returned.
45- /// </summary>
46- /// <param name="id">The value of the primary key for the entity to be found.</param>
47- /// <returns>The entity found, or null.</returns>
48- public virtual TEntity Find ( object id )
49- {
50- Check . NotNull ( id , nameof ( id ) ) ;
51-
52- var tracked = Context . ChangeTracker . GetEntryById < TEntity > ( id ) ;
53-
54- if ( tracked != null )
55- {
56- return tracked . Entity as TEntity ;
57- }
58-
59- var entityDefinition = EntityMapping . GetOrCreateDefinition ( typeof ( TEntity ) ) ;
60- var filter = entityDefinition . CreateIdFilter < TEntity > ( id ) ;
61-
62- var collection = Context . Connection . GetDatabase ( ) . GetCollection < TEntity > ( entityDefinition . CollectionName ) ;
63- var cursor = collection . Find ( filter ) ;
64- var entity = cursor . FirstOrDefault ( ) ;
65-
66- if ( entity != null )
67- {
68- Context . ChangeTracker . SetEntityState ( entity , EntityEntryState . NoChanges ) ;
69- }
70-
71- return entity ;
72- }
73-
37+ }
38+
39+ /// <summary>
40+ /// Finds an entity with the given primary key value. If an entity with the given primary key value
41+ /// is being tracked by the context, then it is returned immediately without making a request to the
42+ /// database. Otherwise, a query is made to the database for an entity with the given primary key value
43+ /// and this entity, if found, is attached to the context and returned. If no entity is found, then
44+ /// null is returned.
45+ /// </summary>
46+ /// <param name="id">The value of the primary key for the entity to be found.</param>
47+ /// <returns>The entity found, or null.</returns>
48+ public virtual TEntity Find ( object id )
49+ {
50+ Check . NotNull ( id , nameof ( id ) ) ;
51+
52+ var tracked = Context . ChangeTracker . GetEntryById < TEntity > ( id ) ;
53+
54+ if ( tracked != null )
55+ {
56+ return tracked . Entity as TEntity ;
57+ }
58+
59+ var entityDefinition = EntityMapping . GetOrCreateDefinition ( typeof ( TEntity ) ) ;
60+ var filter = entityDefinition . CreateIdFilter < TEntity > ( id ) ;
61+
62+ var collection = Context . Connection . GetDatabase ( ) . GetCollection < TEntity > ( entityDefinition . CollectionName ) ;
63+ var cursor = collection . Find ( filter ) ;
64+ var entity = cursor . FirstOrDefault ( ) ;
65+
66+ if ( entity != null )
67+ {
68+ Context . ChangeTracker . SetEntityState ( entity , EntityEntryState . NoChanges ) ;
69+ }
70+
71+ return entity ;
72+ }
73+
74+ /// <summary>
75+ /// Finds an entity with the given primary key value. If an entity with the given primary key value
76+ /// is being tracked by the context, then it is returned immediately without making a request to the
77+ /// database. Otherwise, a query is made to the database for an entity with the given primary key value
78+ /// and this entity, if found, is attached to the context and returned. If no entity is found, then
79+ /// null is returned.
80+ /// </summary>
81+ /// <param name="id">The value of the primary key for the entity to be found.</param>
82+ /// <returns>The entity found, or null.</returns>
83+ public virtual async ValueTask < TEntity > FindAsync ( object id )
84+ {
85+ Check . NotNull ( id , nameof ( id ) ) ;
86+
87+ var tracked = Context . ChangeTracker . GetEntryById < TEntity > ( id ) ;
88+
89+ if ( tracked != null )
90+ {
91+ return tracked . Entity as TEntity ;
92+ }
93+
94+ var entityDefinition = EntityMapping . GetOrCreateDefinition ( typeof ( TEntity ) ) ;
95+ var filter = entityDefinition . CreateIdFilter < TEntity > ( id ) ;
96+
97+ var collection = Context . Connection . GetDatabase ( ) . GetCollection < TEntity > ( entityDefinition . CollectionName ) ;
98+ var cursor = await collection . FindAsync ( filter ) ;
99+ var entity = await cursor . FirstOrDefaultAsync ( ) ;
100+
101+ if ( entity != null )
102+ {
103+ Context . ChangeTracker . SetEntityState ( entity , EntityEntryState . NoChanges ) ;
104+ }
105+
106+ return entity ;
107+ }
108+
109+
74110 /// <summary>
75111 /// Marks the entity for insertion into the database.
76112 /// </summary>
0 commit comments