I've currently built a Visual Studio C# project that saves API data into a database through Entity Framework. Every time I run the project the table in the database is wiped then data is re-added again. This is to stop duplication. However, I'm wondering if there is an alternate route where I don't have to wipe the data but I can just add new data that isn't already there?
Here is the code in my project. Starting from the method in my main class that uses RestSharp to obtain the API data, deserializes to a JSON format, then saves to my DB.
public static void getAllRequestData()
{
var client = new RestClient("[My API URL]");
var request = new RestRequest();
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string rawResponse = response.Content;
AllRequests.Rootobject result = JsonConvert.DeserializeObject<AllRequests.Rootobject>(rawResponse);
using (var db = new TransitionContext())
{
db.RequestDetails.RemoveRange(db.RequestDetails); //Wipes Data
db.RequestDetails.AddRange(result.Operation.Details); //Adds Data
db.SaveChanges();
} //Utilising EF to save data to the DB
}
} //Method that calls and stores API data
Here is the Entity Framework class below, as you can see it just supports one table (dataset).
public class TransitionContext : DbContext
{
private const string connectionString = @"[My Server]";
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<AllRequests.Detail> RequestDetails { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AllRequests.Detail>().HasKey(r => r.Id);
}
}
And here is the DTO class, this holds the the data temrporarily and is used to be structured into a class fit for the API data.
public class AllRequests
{
public class Rootobject
{
public Operation Operation { get; set; }
}
public class Operation
{
public Result Result { get; set; }
public Detail[] Details { get; set; }
}
public class Result
{
public string Message { get; set; }
public string Status { get; set; }
}
public class Detail
{
[Key]
public int Id { get; set; }
public string Requester { get; set; }
public string WorkOrderId { get; set; }
public string AccountName { get; set; }
public string CreatedBy { get; set; }
public string Subject { get; set; }
public string Technician { get; set; }
public string IsOverDue { get; set; }
public long DueByTime { get; set; }
public string Priority { get; set; }
public long CreatedTime { get; set; }
public string IgnoreRequest { get; set; }
public string Status { get; set; }
}
}
Here is the table that is produced (irrelevant data blocked out).
In order to get Entity Framework working, I had to create an ID. This ID does not hold any API data, it simply starts from 1 to however many rows there are. However, WorkOrderId is a unique ID for each row. How would I be able to make this project scan for WorkOrderId's in the table and only add new data where a WorkOrderId wasn't already there?
Because ideally I want to be running this project every 5-10 minutes to keep the table constantly updated, however, at the moment I feel that wiping the table isn't the ideal way to go and is a long process. I would prefer it if I could implement this procedure instead. Any help or pointers would be greatly appreciated.