entity-framework - Azure函数: Can compile but cannot run with custom datalayer library

标签 entity-framework azure azure-functions

我尝试想出一个更好的标题,但未能成功。

问题是我是 Azure 函数的新手,但已经完成了一项简单的写入 SQL Azure 表的工作。现在我尝试构建最简单的基于 Entity Framework 的数据层并上传它。现在它被编译为 .Net 4.6 并使用 EF 6.1.3。

我正在按照此处的第二个答案使用连接字符串 Second answer并检查它是否被正确检索。 更新 - 我还使用了this guide .

删除此 {#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"} 会导致编辑器提示缺少程序集,因此它会找到有问题的 dll。

但是它不会运行 - 它找不到 TestDataLayer.dll。

我只在门户编辑器中运行它(我还没有掌握直接从 Visual Studio 项目进行部署 - 不要笑:P)。

#r "System.Configuration"
#r "System.Data.Entity"
#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"

using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.SqlServer;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Net;
using TestDataLayer;

public static void Run(TimerInfo myTimer, TraceWriter log)
    {
       var connection =  ConfigurationManager.ConnectionStrings["sql_connection"].ConnectionString; 
       using(var db = new SyncDbContext(connection))
            {
                var RK = new RKAzureTest()  {TestField1 = "It finally worked?" };
                db.RKAzureTests.Add(RK);
                db.SaveChanges();
            }
    }       

[DbConfigurationType(typeof(myDBContextConfig))] 
public partial class SyncDbContext : System.Data.Entity.DbContext
{
    public SyncDbContext(string cs) : base(cs) {}
    public DbSet<RKAzureTest> RKAzureTests {get;set;}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
 //       modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }

}

    public  class myDBContextConfig : DbConfiguration
        {
            public myDBContextConfig()
            {
                SetProviderServices("System.Data.EntityClient", 
                System.Data.Entity.SqlServer.SqlProviderServices.Instance);
                SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
            }
        }

这是函数.json:

   {

    "frameworks": {

        "net46":{
                    "dependencies": {

                        "EntityFramework": "6.1.3"
                    }
                }
    }

}

在怀疑Azure Functions不支持.net 4.7.1后,我将dll本身编译为.Net 4.6,并通过Kudu将编译后的dll上传到sharedbin文件夹(检查了路径十几次!) .

这是抛出的错误:

    2018-05-01T11:00:00.012 [Warning] Unable to find assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Are you missing a private assembly file?
2018-05-01T11:00:00.012 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__514732255: Could not load file or assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

不太确定还可以剩下什么 - 我在应用程序设置中使用运行时版本 1.0.11702,因为我发现如果我使用 Beta 版本,生活会变得更加复杂。

如果有人可以向我指出此用例的工作指南(数据库优先、EF 6.1.3 等),我将不胜感激。

感谢您提供的任何帮助!

谢谢:)

最佳答案

转到Azure门户,使用CMD Shell在Azure函数中创建一个名为“bin”的文件夹,将“TestDataLayer.dll”文件上传到刚刚创建的bin文件夹中。

#r "System.Configuration"
#r "System.Data.Entity"
#r "TestDataLayer.dll"

项目结构应该是这样的,

AzureFunctionProjectName001
    bin
       TestDataLayer.dll
    run.csx
    project.json
    project.lock.json
    ...

Azure 函数这次应该能够发现您的库。我相信,EntityFramework 工作得很好。

关于entity-framework - Azure函数: Can compile but cannot run with custom datalayer library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50115519/

相关文章:

azure - 如何在 Azure Functions 中使用多种身份验证方案

azure - 如何使用 Azure.Security.KeyVault 库配置 Azure KeyVault 刷新间隔

javascript - 如何从Azure移动应用服务调用HTTP(Azure Functions)?

Azure 未使用正确的证书

c# - Entity Framework 代码优先 : Using a database that doesn't need to be separately installed?

使用 Microft Azure SDK 时,java 以非零退出值 2 完成

azure - 按 id 数组过滤搜索结果

c# - Entity Framework - 为集合中的成员加载特定的导航属性

c# - MVC 成员资格问题 - C#、EntityFramework

c# - EF 代码第一个 1 对 1 关系错误