Adding Entity Framework/SQL Server to a Function App
When working with function apps, you may want to connect SQL Server to allow for using an API with data persistence.
Setting up Project to Use Entity Framework
Add the following to your .csproj
file, in the section with PackageReference
entries:
And then add the following to allow for copying the project DLL (supports builds in both Windows and Linux):
For setting this up, I’d recommend having at least one Model ready for use. In our case, let’s use a simple example with Entry.cs
:
Now create the Context.cs
and ContextFactory.cs
files in the Models folder:
Now create a Startup.cs
file in root:
Setting up Database
Finally, you’ll need to set up a database for use. Create the database and use the connection string – let’s use SQL Server locally and set this in the Azure Function project’s local.settings.json
:
Once this is set, setting up migrations is the next step. Set the environment variable on your PC:
With this step done, start the application and check to see if your database was updated correctly, and that you can see the tables desired.