Search code examples
mysqlpetapoco

PetaPoco Insert/Update


I have a table with the following field. I am not able to update the existing values in the table. I want to update it based on the SourceId which is not a Primary key (Id is the primary key). Below is the class representation.

I do not want to modify the PetaPoco.cs file. Please help. Thank you.

[TableName("Incident")]
[PrimaryKey("Id", AutoIncrement = true)]
public class Incident
{
    public int Id { get; set; }
    public string SourceId { get; set; }
    public int AgencyId { get; set; }
    public DateTime? OccurredOn { get; set; }
    public DateTime? ImportedOn { get; set; }
    public DateTime? LastUpdatedOn { get; set; }
}

Solution

  • You can select the record using the SourceID and then update it.
    Example:

    var rec = db.FirstOrDefault<Incident>("WHERE SourceID = @0", SourceID);
    rec.AgencyId = NewValueForAgencyId;
    db.Update(rec);