EF6 不保存对数据库的更改
EF6 Not saving changes to Database
完成本教程后:https://www.codeproject.com/Articles/5251396/Use-Azure-Functions-to-process-a-CSV-File-and-impo
我 运行 遇到了问题。我的代码执行完美,但数据没有保存到数据库中。我已经在 azure 和本地实例上尝试了我的 UAT 数据库。
这是我的代码:
namespace func
{
public class Function3
{
[FunctionName("storagetrigger")]
public async Task Run([BlobTrigger("filedump/{name}", Connection = "StorageConn")] Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
if (myBlob.Length > 0)
{
using (var reader = new StreamReader(myBlob))
{
var lineNumber = 1;
var line = await reader.ReadLineAsync();
while (line != null)
{
await ProcessLine(name, line, lineNumber, log);
line = await reader.ReadLineAsync();
lineNumber++;
}
}
}
}
private static async Task ProcessLine(string name, string line, int lineNumber, ILogger log)
{
if (string.IsNullOrWhiteSpace(line))
{
log.LogWarning($"{name}: {lineNumber} is empty.");
return;
}
var parts = line.Split('|');
if (parts.Length != 14)
{
log.LogError($"{name}: {lineNumber} invalid data: {line}.");
return;
}
PackDataModel(parts ,out var item); //Packs data into my model
using (var context = new PartContext())
{
context.Parts.Add(item);
await context.SaveChangesAsync();
log.LogInformation($"{name}: {lineNumber} inserted task: \"{item.PartDescription}\" with id: {item.PartNumber}.");
}
}
}
}
我的数据库上下文:
internal class PartContext : DbContext
{
public PartContext() : base("PartsContext")
{
}
public DbSet<Part> Parts { get; set; }
}
我使用的本地conn字符串:
"PartsContext": "Provider=MySQL Provider;server=localhost;User Id=MyID;password=myPassword;database=mydb;"
我试过的另一个连接字符串:
"PartsContext": "Server=.;Database=sqldb-filedump-mpw-uat;Trusted_Connection=True;"
所以问题似乎是您无法像教程中显示的那样将名称传递给 base。
我通过使用解决了它:
public PartContext() : base(Environment.GetEnvironmentVariable("PartsContext")
完成本教程后:https://www.codeproject.com/Articles/5251396/Use-Azure-Functions-to-process-a-CSV-File-and-impo
我 运行 遇到了问题。我的代码执行完美,但数据没有保存到数据库中。我已经在 azure 和本地实例上尝试了我的 UAT 数据库。
这是我的代码:
namespace func
{
public class Function3
{
[FunctionName("storagetrigger")]
public async Task Run([BlobTrigger("filedump/{name}", Connection = "StorageConn")] Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
if (myBlob.Length > 0)
{
using (var reader = new StreamReader(myBlob))
{
var lineNumber = 1;
var line = await reader.ReadLineAsync();
while (line != null)
{
await ProcessLine(name, line, lineNumber, log);
line = await reader.ReadLineAsync();
lineNumber++;
}
}
}
}
private static async Task ProcessLine(string name, string line, int lineNumber, ILogger log)
{
if (string.IsNullOrWhiteSpace(line))
{
log.LogWarning($"{name}: {lineNumber} is empty.");
return;
}
var parts = line.Split('|');
if (parts.Length != 14)
{
log.LogError($"{name}: {lineNumber} invalid data: {line}.");
return;
}
PackDataModel(parts ,out var item); //Packs data into my model
using (var context = new PartContext())
{
context.Parts.Add(item);
await context.SaveChangesAsync();
log.LogInformation($"{name}: {lineNumber} inserted task: \"{item.PartDescription}\" with id: {item.PartNumber}.");
}
}
}
}
我的数据库上下文:
internal class PartContext : DbContext
{
public PartContext() : base("PartsContext")
{
}
public DbSet<Part> Parts { get; set; }
}
我使用的本地conn字符串:
"PartsContext": "Provider=MySQL Provider;server=localhost;User Id=MyID;password=myPassword;database=mydb;"
我试过的另一个连接字符串:
"PartsContext": "Server=.;Database=sqldb-filedump-mpw-uat;Trusted_Connection=True;"
所以问题似乎是您无法像教程中显示的那样将名称传递给 base。
我通过使用解决了它:
public PartContext() : base(Environment.GetEnvironmentVariable("PartsContext")