fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
580 stars 146 forks source link

Will this provider generate types based on DB structure? #212

Open xperiandri opened 8 years ago

xperiandri commented 8 years ago

Will this provider behave similar to Entity Framework and generate types for database structure?

colinbull commented 8 years ago

Yes, see http://fsprojects.github.io/SQLProvider/ for more info. The major difference is the types are erased not generated.

xperiandri commented 8 years ago

I can't understand how to achieve what I want. I want to return some data from DB from ASP.NET WebAPI. If I return items from table they appear to be SqlEntity not a generated type. And I don't want to specify columns one by one and return Tuple.

xperiandri commented 8 years ago

Because providing type safe access to columns of entity is good for F# code but not interopability.

colinbull commented 8 years ago

Sorry, missed your replies to this.

So I guess you want to serialize the result directly from a call to the type provider?

This isn't possible as the types that are provided by the type provider are erased at run-time. This essentially means that all of the metadata infrastructure that most serializers require is not available.

However we have a work around by using the MapTo method on SqlEntity, see. here for an example, this simply works by matching properties on a name basis, but it is possible to override how this gets assigned.

Thorium commented 8 years ago

If you do this:

let itms = query { ... } |> Seq.toArray //array of entities
let res = itms |> Array.map(fun i -> i.ColumnValues |> Map.ofSeq)

...and now you have quite nice array of maps that should be fairy simple to transfer to JSON (actually I think Newtonsoft can already serialize F#-map to JSON automatically).