Bellatrix
Bellatrix

Reputation: 39

Automate the execution of a C# code that uses Entity Framework to treat data?

I have code that uses Entity Framework to treat data (retrieves data from multiple tables then performs operations on it before saving in a SQL database). The code was supposed to run when a button is clicked in an MVC web application that I created. But now the client wants the data treatment to run automatically every day at a set time (like an SSIS package). How do I go about this?

Upvotes: 1

Views: 224

Answers (2)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89051

But now the client wants the data treatment to run automatically every day at a set time (like an SSIS package). How do I go about this?

In addition to adding a job scheduler to your MVC application as @Pac0 suggests, here are a couple of other options:

Leave the code in the MVC project and create an API endpoint that you can invoke on some sort of schedule. Give the client a PowerShell script that calls the API and let them take it from there.

Or

Refactor the code into a .DLL or copy/paste it into a console application that can be run on a schedule using the Windows Scheduler, SQL Agent or some other external scheduler.

Upvotes: 4

Pac0
Pac0

Reputation: 23149

You could use some tool/lib that does this for you. I could recommend Hangfire, it works fine (there are some others, I have not tried them).

The example on their homepage is pretty explicit :

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Recurring!"),
    Cron.Daily);

The above code needs to be executed once when your application has started up, and you're good to go. Just replace the lambda by a call to your method.

Adapt the time parameter on what you wish, or even better: make it configurable, because we know customers like to change their mind.

Hangfire needs to create its own database, usually it will stay pretty small for this kind of things. You can also monitor if the jobs ran well or not, and check no the hangfire server some useful stats.

Upvotes: 1

Related Questions