通过首先使用代码asp.net mvc删除旧(过去的日期)条目来保持表小的最佳方法

时间:2022-07-15 22:28:05

I would like to know the best way to remove old entries (with a date from past) from a table that is created using the 'SpaceEvent' entity with EF code first. The objective is to keep the table as small as possible.

我想知道从使用带有EF代码的'SpaceEvent'实体创建的表中删除旧条目(带有过去日期)的最佳方法。目标是保持桌子尽可能小。

**Specifically I want to remove entries that are in the past (ex. anything from yesterday or before)

**具体来说,我想删除过去的条目(例如,昨天或之前的任何内容)

Do I create a service and run it independently of my website or do I create a program in my website solution that somehow gets started when my website gets published that runs every N minutes, or do I run something from Sql Server? What's the best solution here.

我是否创建了一个服务并独立于我的网站运行它,或者我在我的网站解决方案中创建一个程序,当我的网站每隔N分钟发布一次,或者我从Sql Server运行某些东西时,它会以某种方式启动?什么是最好的解决方案。

Here is my entity

这是我的实体

public class SpaceEvent
{
    [Key]
    public int SpaceEventId { get; set; }
    //public string Title { get; set; }
    [Index]
    //research more about clustered indexes to see if it's really needed here
    //[Index(IsClustered = true, IsUnique = false)]
    [Required]
    public DateTime DateTimeScheduled { get; set; }

    [Required]
    public int AppointmentLength { get; set; }
    public int StatusEnum { get; set; }

    public virtual ICollection<Student.RegisteredStudent> RegisteredStudents { get; set; }
    public int RegisteredStudentCount { get; set; }

    public byte[] LastRegisteredStudentImage { get; set; } // represents the last student to register for the class
    public string LastRegisteredStudentPhrase { get; set; }

    [Index]
    public int SpaceRefId { get; set; }

    [ForeignKey("SpaceRefId")]
    public virtual Space Space { get; set; }
}

2 个解决方案

#1


0  

Try using Hangfire. It's built to specifically handle these types of scenarios. You can simply set up a recurring task and it will run on any interval you request. Best part is, it runs as part of your web application so there's no need to create messy windows services and it deploys easily.

尝试使用Hangfire。它专为处理这些类型的场景而构建。您可以简单地设置定期任务,它将在您请求的任何时间间隔内运行。最好的部分是,它作为Web应用程序的一部分运行,因此不需要创建凌乱的Windows服务,并且它可以轻松部署。

Hangfire Website

#2


0  

It is hard to tell you "the best solution". Your question has an opinion-based answer.

很难告诉你“最好的解决方案”。您的问题有基于意见的答案。

However, I think the easiest way is creating and T-SQL script, and run it directly in SQL SERVER. The problem with this solution, is that you need to do this manually. If you want something automatic, you could use a service, like Azure Web Jobs. So, you can create a console-like C# program, and schedule it to run in a specific interval of time.

但是,我认为最简单的方法是创建和T-SQL脚本,并直接在SQL SERVER中运行它。此解决方案的问题在于您需要手动执行此操作。如果您想要自动化的东西,可以使用Azure Web Jobs等服务。因此,您可以创建类似控制台的C#程序,并将其安排在特定的时间间隔内运行。

To see more information about Azure Web Jobs, take a look at https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/

要查看有关Azure Web作业的更多信息,请查看https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/

#1


0  

Try using Hangfire. It's built to specifically handle these types of scenarios. You can simply set up a recurring task and it will run on any interval you request. Best part is, it runs as part of your web application so there's no need to create messy windows services and it deploys easily.

尝试使用Hangfire。它专为处理这些类型的场景而构建。您可以简单地设置定期任务,它将在您请求的任何时间间隔内运行。最好的部分是,它作为Web应用程序的一部分运行,因此不需要创建凌乱的Windows服务,并且它可以轻松部署。

Hangfire Website

#2


0  

It is hard to tell you "the best solution". Your question has an opinion-based answer.

很难告诉你“最好的解决方案”。您的问题有基于意见的答案。

However, I think the easiest way is creating and T-SQL script, and run it directly in SQL SERVER. The problem with this solution, is that you need to do this manually. If you want something automatic, you could use a service, like Azure Web Jobs. So, you can create a console-like C# program, and schedule it to run in a specific interval of time.

但是,我认为最简单的方法是创建和T-SQL脚本,并直接在SQL SERVER中运行它。此解决方案的问题在于您需要手动执行此操作。如果您想要自动化的东西,可以使用Azure Web Jobs等服务。因此,您可以创建类似控制台的C#程序,并将其安排在特定的时间间隔内运行。

To see more information about Azure Web Jobs, take a look at https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/

要查看有关Azure Web作业的更多信息,请查看https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/