简体   繁体   English

如何在 Azure Functions 中使用 Entity Framework Core

[英]How do I use Entity Framework Core in Azure Functions

I am trying to make a simple Web API with Azure Functions (Http trigger).我正在尝试使用 Azure 函数(Http 触发器)制作一个简单的 Web API。
In my function I want to access a SQL Server Database.在我的函数中,我想访问 SQL Server 数据库。
I would like to retrieve my data from the database using Entity Framework Core.我想使用 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实体框架核心 3.1.0
  • Azure Functions v3 (.NET Core) Azure Functions v3 (.NET Core)

I have an existing database, therefor I want to do reverse engineering using the Scaffold-DbContext command.我有一个现有的数据库,因此我想使用 Scaffold-DbContext 命令进行逆向工程。
In order to do this I have added the following packages to my Azure Functions v3 project created in Visual Studio:为此,我将以下包添加到我在 Visual Studio 中创建的 Azure Functions v3 项目中:

  • Microsoft.EntityFrameworkCore.SqlServer (3.1.0) Microsoft.EntityFrameworkCore.SqlServer (3.1.0)
  • Microsoft.EntityFrameworkCore.Tools (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:在包管理器控制台中运行 Scaffold-DbContext 命令时,我收到以下错误:

在此处输入图片说明

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.构建项目时,项目 dll 会生成到bin\\Debug\\netcoreapp3.0\\bin文件夹,但是当您尝试运行 Scaffold-DbContext 命令时,它会在bin\\Debug\\netcoreapp3.0文件夹中查找它。

Solution.解决方案。 Just copy the project dll to that folder and then it works.只需将项目 dll 复制到该文件夹​​,然后它就可以工作了。

For thr error, try to add对于 thr 错误,尝试添加

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

to your .csproj file.到您的 .csproj 文件。 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.然后将此项目导入您的第一个项目,这似乎是 azure-function 的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Entity Framework Core 2.0的模型内部使用OnModelCreating? - How Do I Use OnModelCreating Inside the Model itself in Entity Framework Core 2.0? 如何在 C# 实体框架核心中使用 class 的构造函数,并将 class 作为字段? - How do I use constructer for a class with a class as a field in C# Entity Framework Core? 如何在使用 Entity Framework Core 的 LINQ 更新语句中使用 CURRENT_TIMESTAMP? - How do I use CURRENT_TIMESTAMP in a LINQ update statement using Entity Framework Core? 实体框架核心2.0功能如何工作 - Entity framework core 2.0 Functions how to work 如何在实体框架核心中使用泛型类型? - How can I use a generic type with entity framework core? 如何删除 Entity Framework Core 中的多行? - How do I delete multiple rows in Entity Framework Core? 实体框架核心运行缓慢-如何使其更快? - Entity Framework Core is slow - how do I make it faster? 如何删除 Entity Framework.Core 中的一组记录? - How do I delete a set of records in Entity Framework .Core? 如何更改Entity Framework Core 2.2中的ILazyLoader实现? - How do I change the ILazyLoader implementation in Entity Framework Core 2.2? 如何在 Entity Framework Core 中将连接字符串传递给 DBContext? - How do I pass connection string to DBContext in Entity Framework Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM