简体   繁体   中英

How do I use Entity Framework Core in Azure Functions

I am trying to make a simple Web API with Azure Functions (Http trigger).
In my function I want to access a SQL Server Database.
I would like to retrieve my data from the database using Entity Framework Core.
I would prefer to to use the newest versions available at the time of posting this:

  • Entity Framework Core 3.1.0
  • Azure Functions v3 (.NET Core)

I have an existing database, therefor I want to do reverse engineering using the Scaffold-DbContext command.
In order to do this I have added the following packages to my Azure Functions v3 project created in Visual Studio:

  • Microsoft.EntityFrameworkCore.SqlServer (3.1.0)
  • Microsoft.EntityFrameworkCore.Tools (3.1.0)

When running the Scaffold-DbContext command in the Package Manager Console I get the following error:

在此处输入图片说明

What am I doing wrong?
What can I do to fix this?

I found the solution to the problem.

When the project is build the project dll gets generated to the bin\\Debug\\netcoreapp3.0\\bin folder but when you try and run the Scaffold-DbContext command it looks for it in the bin\\Debug\\netcoreapp3.0 folder.

Solution. Just copy the project dll to that folder and then it works.

For thr error, try to add

<PropertyGroup>               
    <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>

to your .csproj file. Test whether it can be solved.

If it doesn't work, please try to create a new project under the same solution and ran the entity framework commands in that project. That should work. Then import this project to your first project, this seems an issue with azure-function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM