Visual Studio中Azure函数中的实体框架 [英] Entity Framework in Azure Function in Visual Studio

查看:85
本文介绍了Visual Studio中Azure函数中的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中,我有一个用C#编写的Azure函数,应该使用实体框架(EF6)从Azure SQL数据库中读取。



我不能使实体框架正常工作。当我发布Azure函数时,出现错误:


该上下文在代码优先模式下与从EDMX生成的代码一起使用数据库优先或模型优先开发的文件。这将无法正常工作。若要解决此问题,请不要删除引发此异常的代码行。如果希望使用数据库优先或模型优先,请确保在启动项目的app.config或web.config中包含Entity Framework连接字符串。如果要创建自己的DbConnection,请确保它是EntityConnection而不是其他类型的DbConnection,并将其传递给采用DbConnection的基本DbContext构造函数之一。要了解有关代码优先,数据库优先和模型优先的更多信息,请参见此处的实体框架文档:



对于数据库,我建议您修改生成的 Model.Content.cs 并按如下所示配置DbContext:

 公共部分类实体:DbContext 
{
public Entities():base(ConfigurationManager.ConnectionStrings [ Entities]。ConnectionString)
{

}
}

注意:您可以修改t4模板,并确保从数据库更新模型后,对DbContext的修改不会被覆盖。



对于开发人员,您可以将连接字符串设置为 local下。 settings.json 文件如下:

  ConnectionStrings:{
Entities :元数据= res://*/Model1.csdl | res://*/Model1.ssdl | res://*/Model1.msl; provider = System.Data.SqlClient; provider连接字符串='数据源= {server-name} .database.windows.net;初始目录= {db-name};持久安全性信息= True;用户ID = {username}; password = {password}; MultipleActiveResultSets = True; App = EntityFramework'
}

在发布到Azure之前,您可以创建与配置相同的连接字符串在 local.settings.json 文件下,并按如下所示设置连接字符串:





此外,您可以遵循类似的问题有关在Azure函数中使用EF数据库优先的方法。


In Visual Studio I have an Azure Function, written in C#, which is supposed to read from an Azure SQL database using Entity Framework (EF6).

I cannot get Entity Framework to work. When I publish the Azure Function I get the error:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

None of this worked.

I also tried to add a project.json file in Azure as recommended by many websites but that didn’t change anything.

Here is the C#.

public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("*/100 * * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        try {
            using (var qc = new quotecandyEntities()) {

                if (qc.Users.Any()) {
                    log.Info($"The last user is {qc.Users.Last().Email}.");

                } else {
                    log.Info("No users found in database.");
                }
            }
        } catch (Exception ex) {
            log.Error($"Error: {ex.Message}");
        }
    }
}

解决方案

According to your description, I assumed that you are using Database First or Model First, and you configured the wrong Entity Framework connection string for your Entity Data Model. I tested and found that if my directly copy the connection string from Azure Portal while I am using Database First, I would encounter the similar issue you provided as follows:

For Database First, I would recommend you modify the generated Model.Content.cs and configure your DbContext as follows:

public partial class Entities : DbContext
{
    public Entities():base(ConfigurationManager.ConnectionStrings["Entities"].ConnectionString)
    {

    }
}

Note: You could modify the t4 template and make sure you modification for your DbContext does not overridden after you updated the model from your database.

For dev, you could set your connection string under local.settings.json file as follows:

"ConnectionStrings": {
  "Entities": "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source={server-name}.database.windows.net;initial catalog={db-name};persist security info=True;user id={username};password={password};MultipleActiveResultSets=True;App=EntityFramework'"
}

Before publishing to azure, you could create a connection string with the same as you configured under local.settings.json file and set the connection string as follows:

Moreover, you could follow this similar issue about the approach for using EF Database First in Azure Function.

这篇关于Visual Studio中Azure函数中的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆