# Best pratices SQL databases

**URL:** <https://forums.fsharp.org/t/best-pratices-sql-databases/3501>\
**Category:** General\
**Tags:** beginners\
**Created:** [January 6, 2024, 8:06pm UTC](https://forums.fsharp.org/t/best-pratices-sql-databases/3501 "2024-01-06T20:06:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre-dasilva](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/andre-dasilva/32/1276_2.png) [@andre-dasilva](https://forums.fsharp.org/u/andre-dasilva)\
**Post date:** [January 6, 2024, 8:06pm UTC](https://forums.fsharp.org/t/best-pratices-sql-databases/3501/1 "2024-01-06T20:06:34Z")

</div>

Hi everyone, what is like the go to way to interact with SQL databases in F#?

I have seen the recommandations on: [Guide - Data Access | The F# Software Foundation](https://fsharp.org/guides/data-access/). Looks like there are multiple ways but not really a goto way. And a lot of the project seems not really maintained. Or look like hobby projects.

I also know that i could use the entity framework. But for me that code just ends up to be like c#. and at that point i could as well write everythin in c#.

But I would love if there was a nice way to handle SQL databases in F#. I love the giraffe framework and it would be amazing if there was a library that is well supported that could handle database interactions.

Best regards

---

<div class="post-metadata">

**Author:** ![charlesroddie](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/charlesroddie/32/45_2.png) [@charlesroddie](https://forums.fsharp.org/u/charlesroddie)\
**Post date:** [January 7, 2024, 2:51pm UTC](https://forums.fsharp.org/t/best-pratices-sql-databases/3501/2 "2024-01-07T14:51:14Z")

</div>

In my opinion, the SQL language (despite several flaws) is the best way to query an SQL database. SSDT projects in VS are also very good, allowing you to define databases in source control with some (incomplete but still useful) degree of type-safety.

[ADO.Net](http://ADO.Net) is the fastest way to access queries defined in SQL (stored procedures, UDFs, etc.).

For a typed link between the two you can use [GitHub - cmeeren/Facil: Facil generates F# data access source code from SQL queries and stored procedures. Optimized for developer happiness.](https://github.com/cmeeren/Facil) . The approach is:

1. deploy the database to localdb when generating code
2. use Facil to generate [ADO.Net](http://ADO.Net) code to access tables, stored procedures, UDFs.
3. do something to guarantee that the [ADO.Net](http://ADO.Net) code matches the sql files. You can have a test to generate them and check that the code is the same, but to save time we just put a hash of sql files in the generated code with a test to ensure that we don’t accidentally change the sql files without generating the code.

I don’t use Facil but it looks very robust. We use a small library to do the same (just 500LOC and I could make a gist of it if interested).

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [January 8, 2024, 4:49pm UTC](https://forums.fsharp.org/t/best-pratices-sql-databases/3501/3 "2024-01-08T16:49:03Z")

</div>

We’re fairly happy with using [SqlHydra](https://github.com/JordanMarr/SqlHydra) for our use case where there’s a pre-existing database and we want to generate F# code to query it. It’s well-maintained, though it does feel like a lot hinges on the project owner. There’s a lot less “magic” to SqlHydra than EntityFramework, since it pretty much just generates type-checked sql queries for you; it’s not an ORM.
