我正在使用Entity Framework / Azure Functions来尝试基于this csvs的GitHub存储库更新数据库。
过程
客观
我想改进第4步。我不想完全删除表然后重新创建所有内容,而是想做某种比较,然后从那里向上插入(或删除)。
代码:
[FunctionName("CsvQueueProcessor")]
public async void Run([QueueTrigger("csvqueue", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
{
var context = new ChadwickDbContext(Environment.GetEnvironmentVariable("DefaultConnectionString"));
var csvEntity = JsonConvert.DeserializeObject<CsvProcessorEntity>(myQueueItem);
if (csvEntity.FileName.ToLower() != "allstarfull.csv") return;
log.LogInformation($"About to get the file from blob storage: {csvEntity.FileName}");
var container = CloudBlobClient.GetContainerReference("csvs");
await container.CreateIfNotExistsAsync();
var blob = container.GetBlobReference(csvEntity.FileName);
var stream = await blob.OpenReadAsync();
var csv = new CsvReader(new StreamReader(stream));
while(csv.Read())
{
var record = csv.GetRecord<AllStarFull>();
context.AllStarFull.Add(record);
}
await context.SaveChangesAsync();
}
一切正常!但是,我将如何进行区分?是否有任何实体框架功能允许某种比较或如果记录已经存在则“不采取任何行动”?我遇到了this线程,但我认为这不是我想要的。
public class AllStarFull
{
[Ignore]
public int Id { get; set; }
[Name("playerID")]
public string PlayerId {get;set;}
[Name("yearID")]
public int YearId {get;set;}
[Name("gameNum")]
public int? GameNumber {get;set;}
[Name("teamID")]
public string TeamId {get;set;}
[Name("lgID")]
public string LeagueId {get;set;}
[Name("GP")]
public int? PlayedInGame { get; set; }
[Name("startingPos")]
public int? StartingPosition { get; set; }
}