After following this tutorial: https://www.codeproject.com/Articles/5251396/Use-Azure-Functions-to-process-a-CSV-File-and-impo
I have run into an issue. My code executes perfectly, but the data is not saved to the db. I have tried my UAT database on azure as well as my local instance.
Here is the code I have:
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}.");
}
}
}
}
My DB context:
internal class PartContext : DbContext
{
public PartContext() : base("PartsContext")
{
}
public DbSet<Part> Parts { get; set; }
}
The local conn string I used:
"PartsContext": "Provider=MySQL Provider;server=localhost;User Id=MyID;password=myPassword;database=mydb;"
Another conn string I tried:
"PartsContext": "Server=.;Database=sqldb-filedump-mpw-uat;Trusted_Connection=True;"
CodePudding user response:
So the issue seems to have been that you cant pass a name to base as the tutorial displayed.
I resolved it by using:
public PartContext() : base(Environment.GetEnvironmentVariable("PartsContext")