Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用SaveChanges()时显示Entity Framework Core为PostgreSQL生成的数据库查询_C#_Postgresql_Entity Framework Core - Fatal编程技术网

C# 调用SaveChanges()时显示Entity Framework Core为PostgreSQL生成的数据库查询

C# 调用SaveChanges()时显示Entity Framework Core为PostgreSQL生成的数据库查询,c#,postgresql,entity-framework-core,C#,Postgresql,Entity Framework Core,我已经为postgreSQL创建了实体框架核心的第一个数据库实现。我尝试了一个简单的项目创建和保存,但得到了一个语法错误。我希望能够查看生成的SQL语句,该语句可能会为我提供有关该问题的线索。调试时如何启用这种类型的输出?一般来说,我是EF的新手,所以回答问题时,请明确具体的要求 下面是我的测试功能: public async Task<string> FunctionHandler(string input, ILambdaContext context) { try

我已经为postgreSQL创建了实体框架核心的第一个数据库实现。我尝试了一个简单的项目创建和保存,但得到了一个语法错误。我希望能够查看生成的SQL语句,该语句可能会为我提供有关该问题的线索。调试时如何启用这种类型的输出?一般来说,我是EF的新手,所以回答问题时,请明确具体的要求

下面是我的测试功能:

public async Task<string> FunctionHandler(string input, ILambdaContext context)
{
    try
    {

        using (var db = new loyaltyContext())
        {
            var customer = new Customer
            {
                BusinessPartnerId = 4332482,
                StoreJoined = "chadstone",
                CreatedDateTime = DateTime.Now,
                CustomerType = "STANDARD",
                DateOfBith = DateTime.Now.Date,
                StoreJoinedDate = DateTime.Now.Date,
                UpdatedDateTime = DateTime.Now,
                FirstName = "Joe",
                LastName = "Test",
                Gender = "M",
                OrganisationName = "Australian Pharmaceutical Industries"
            };

            customer.Membership.Add(new Membership { BusinessPartnerId = 4332482, DateCreated = DateTime.Now, LoyaltyCustomerId = 1234, McaId = "2701000" });
            db.Customer.Add(customer);
            var count = db.SaveChanges();
            Console.WriteLine("${count} changes");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }

    return input?.ToUpper();
}
但是,调试输出仍然没有显示任何内容:


最近我写了一篇关于这个的博客文章。您正在使用.NETCore吗

在startup.cs类和ConfigureServices方法中启用敏感数据日志记录

 services.AddDbContext<DutchContext>(
            cfg =>
            {
                cfg.UseSqlServer(_config.GetConnectionString("DutchConnectionString")).EnableSensitiveDataLogging();
            }
            );
现在运行应用程序,如果选择web服务器作为输出源,则可以在输出窗口中查看sql


谢谢。我将用我添加的内容更新我的帖子,但调试输出中仍然没有记录任何内容。我没有更新appsettings.json,因为我没有。我正在Lambda函数(AWS)中测试这一点,但我使用的是.NET内核。是否有一种方法可以在代码中设置此值以进行测试?请尝试显示Web服务器的输出,而不是调试。看看这是否有帮助?只需使用下拉列表。虽然如果记录器没有配置为记录ef核心命令,我看不出它是这样工作的。在哪里指定连接字符串或类似的东西?我猜应该有一些配置?因为它是Lambda函数,所以我可以添加配置作为环境变量。这里没有appSettings.json。有没有一种方法可以将配置硬编码为测试目的?或者使用另一种方法输出生成的查询?尝试将其添加到您的环境变量键:“Logging:LogLevel:Microsoft.EntityFrameworkCore.Database.Command”VALUE:“Information”Thank you@Eamon,我无法相信在Microsoft文档中找到这个简单的解决方案会如此困难
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseNpgsql("xxxxxxx").EnableSensitiveDataLogging(true);
}
 services.AddDbContext<DutchContext>(
            cfg =>
            {
                cfg.UseSqlServer(_config.GetConnectionString("DutchConnectionString")).EnableSensitiveDataLogging();
            }
            );
"Logging": {
    "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.EntityFrameworkCore.Database.Command": "Information"
    }
}