Search code examples
c#asp.net-core-2.1

.NET Core 2.1 - Entity Framework: Check for duplicates before adding to DB


In my SQL Server DB, I have a table of IOT data (nearing 1,000,000 records). When the app receives inbound readings, I want to check that the DB doesn't already have a reading for that device with the same timestamp. What is the fastest way to check for records with matching properties?

Model

public class Reading
{
    public int Id { get; set; }
    public double Measurement { get; set; }
    public int DeviceId { get; set; }
    public Device Device { get; set; }
    public DateTime Timestamp { get; set; }
}

AddReading Method

public class ReadingRepository
{
    private readonly DataContext _context;

    public ReadingRepository(DataContext context)
    {
        _context = context;
    }

    public void AddReading(Reading reading)
    {
        // my proposed method... is there a better way?
        if (!_context.Readings.Any(r =>
            r.DeviceId == reading.DeviceId,
            r.Timestamp == reading.Timestamp))
            _context.Readings.Add(reading);
    }
}

Solution

  • The fastest way is to insert and have a unique index blow and react to the error message.

    Alternative do use a stored procedure.

    Anything you do with EF will per definition NOT be fastest choice. And yes, this does not mean abandoning it all - EF is good for 80% to 95% of operations. Just bypass it for this one.