How to connect BlobTrigger Stream of an Sqlite file to Entity Framework Core?

Tyler Mason 50 Reputation points
2024-01-02T03:34:41.5066667+00:00

I'm trying to setup an Azure Function in .NET 8 Isolated with a Blob Trigger to read/write to Sqlite files as they're uploaded to the blob.

My issue currently, is I can't seem to figure out how to connect the Function's [BlobTrigger(~)] Stream stream argument to the EF DbContext.

When I search for this, the most common thing to come up is the Blob I/O for making an SqliteBlob stream, but that takes a connection string, which isn't given from the Function arguments, and doesn't have an option to create the object using another Stream object. Also even if I'm able to make an SqliteBlob object, I'm still not sure on how that would connect to the DbContext.


Here are my cs files I have so far for this (the .anki2 file extension is the Sqlite file):

Function1.cs.txt

Anki2Context.cs.txt

Anki2Controller.cs.txt

Developer technologies .NET Entity Framework Core
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-01-02T19:16:54.9233333+00:00

    you use the dbcontext to get the underlying connection, then use the connection with the sample code.

    var connection = dbContext.Database.GetConnection();

    you can use EF to create the row and get the rowid for the SqliteBlob() call


  2. Tyler Mason 50 Reputation points
    2024-01-03T02:07:37.9033333+00:00

    Did some more research and playing around, and figured out a solution.

    For the function, I realized that the BlobTrigger can pass other objects besides a Stream. Specifically a BlockBlobClient, which I used to download the file to the local AppData folder (Azure functions has local storage, but I haven't tested this outside of Azurite yet).

    In the DbContext, I added an override to OnConfiguring to allow Sqlite access. Then I was able to pass in the local Sqlite file path and access everything without issue.


    Here are the updated cs files I was using for reference:

    Function1.cs.txt

    Anki2Context.cs.txt

    Anki2Controller.cs.txt

    0 comments No comments

  3. MayankBargali-MSFT 70,936 Reputation points Moderator
    2024-01-03T12:05:28.7933333+00:00

    @Tyler Mason I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others", I'll repost your solution in case you'd like to "Accept" the answer.

    Issue:

    You observed that you cannot find a way to connect the Function's [BlobTrigger(~)] Stream stream argument to the EF DbContext. When you search for this, the most common thing to come up is the Blob I/O for making an SqliteBlob stream, but that takes a connection string, which isn't given from the Function arguments, and doesn't have an option to create the object using another Stream object. Also even if I'm able to make an SqliteBlob object, I'm still not sure on how that would connect to the DbContext.

    Solution:

    You did some more research and found that for the function, you can pass other objects for BlobTrigger other besides a Stream. Specifically a BlockBlobClient, which you used to download the file to the local AppData folder

    In the DbContext, you added an override to OnConfiguring to allow Sqlite access. Then you was able to pass in the local Sqlite file path and access everything without issue.

    Thank you again for your time and patience throughout this issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.