[英]Entity Framework Core is trying to insert duplicated records when SaveChanges() is called
[英]Show database query generated by Entity Framework Core for PostgreSQL when SaveChanges() called
我已經為 postgreSQL 創建了實體框架核心的數據庫第一個實現。 我嘗試了一個簡單的項目創建和保存,但出現語法錯誤。 我希望能夠看到生成的 SQL 語句,這可能會給我關於這個問題的線索。 調試時如何啟用這種類型的 output? 一般來說,我是 EF 的新手,所以對於答案,請具體明確要求。
下面是我的測試 function:
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();
}
當前錯誤:
Npgsql.PostgresException (0x80004005): 42601: syntax error at end of input
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 973
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 407
at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 298
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1195
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1076
at Npgsql.NpgsqlCommand.ExecuteReader() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1067
at AWSLambdaLCSTest.Function.GetAll(NpgsqlConnection connection, String schema, String table) in C:\Users\JMatson\source\repos\AWSLambdaLCSTest\AWSLambdaLCSTest\Function.cs:line 109
at AWSLambdaLCSTest.Function.FunctionHandler(String input, ILambdaContext context) in C:\Users\JMatson\source\repos\AWSLambdaLCSTest\AWSLambdaLCSTest\Function.cs:line 53
Exception data:
Severity: ERROR
SqlState: 42601
MessageText: syntax error at end of input
Position: 22
File: scan.l
Line: 1126
Routine: scanner_yyerror
編輯:通過按照建議嘗試以下設置進行更新:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("xxxxxxx").EnableSensitiveDataLogging(true);
}
但是調試 output 仍然沒有顯示任何內容:
我最近寫了一篇關於這個的博客文章。 您使用的是 .net 內核嗎?
https://eamonkeane.dev/how-to-view-sql-generated-by-entity-framework-core-using-logging/
在您的 startup.cs class 和 ConfigureServices 方法中啟用敏感數據日志記錄。
services.AddDbContext<DutchContext>(
cfg =>
{
cfg.UseSqlServer(_config.GetConnectionString("DutchConnectionString")).EnableSensitiveDataLogging();
}
);
接下來配置日志記錄級別以將 ef 包含在您的配置文件中。 通常是appsettings.json
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
}
}
Now run the application and you can view the sql in your output window if you select the web server as source of the output.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.