@@ -236,6 +236,37 @@ type public SqlDataContext (typeName, connectionString:string, providerType:Data
236236 if provider.CloseConnectionAfterQuery then con.Close()
237237 entity
238238
239+ member this.GetIndividualAsync ( table , id ) =
240+ task {
241+ use con = provider.CreateConnection( connectionString) :?> System .Data .Common .DbConnection
242+ let table = Table.FromFullName table
243+ // this line is to ensure the columns for the table have been retrieved and therefore
244+ // its primary key exists in the lookup
245+ let columns = provider.GetColumns( con, table)
246+ let pk =
247+ match provider.GetPrimaryKey table with
248+ | Some v -> columns.[ v]
249+ | None ->
250+ // this fail case should not really be possible unless the runtime database is different to the design-time one
251+ failwithf " Primary key could not be found on object %s . Individuals only supported on objects with a single primary key." table.FullName
252+ use com = provider.CreateCommand( con, provider.GetIndividualQueryText( table, pk.Name)) :?> System .Data .Common .DbCommand
253+ if commandTimeout.IsSome then
254+ com.CommandTimeout <- commandTimeout.Value
255+ //todo: establish pk SQL data type
256+ com.Parameters.Add ( provider.CreateCommandParameter( QueryParameter.Create( " @id" , 0 , pk.TypeMapping), id)) |> ignore
257+ if con.State <> ConnectionState.Open then
258+ do ! con.OpenAsync()
259+ if con.State <> ConnectionState.Open then // Just ensure, as not all the providers seems to work so great with OpenAsync.
260+ if con.State <> ConnectionState.Closed && provider.CloseConnectionAfterQuery then con.Close()
261+ con.Open()
262+ use! reader = com.ExecuteReaderAsync()
263+ let! entities = ( this :> ISqlDataContext ). ReadEntitiesAsync ( table .FullName , columns , reader )
264+ let entity = entities |> Seq.exactlyOne
265+ reader.Close()
266+ if provider.CloseConnectionAfterQuery then con.Close()
267+ return entity
268+ }
269+
239270 member this.ReadEntities ( name : string , columns : ColumnLookup , reader : IDataReader ) =
240271 [| while reader.Read() do
241272 let e = SqlEntity( this, name, columns, reader.FieldCount)
0 commit comments