0

As am updating the data which is already present in database and that table has the columns like CreatedBy, CreatedDate, ModifiedBy, ModifiedDate. CreatedBy and CreatedDate is inserted when the new row is created but while updating, I don't need to update CreatedBy and CreatedDate and I need to modify only ModifiedBy and ModifiedDate.

As shown in the below code, it will update the CreatedBy as null and CreatedDate as like 01/01/0001 something like this.

_Context.UserProfile.Update(userProfile);

and my model is like,

public class MyModelUserProfile
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
}

Is there any way to avoid updating CreatedBy and CreatedDate column? I can do that by getting that particular record by its email id but i don't want to hit the database once again for this.

Chandan Y S
  • 968
  • 11
  • 21
  • *As shown in the below code* -- That doesn't show why these properties/fields should be updated. Depends on how `userProfile` is modified. BTW, where are the date properties? – Gert Arnold Jan 22 '19 at 09:23
  • 1
    Use the answer from the link, but specify `Ignore` rather than `Throw`. – Ivan Stoev Jan 22 '19 at 09:57

1 Answers1

2

You do not need change CreatedBy and CreatedDate properties in your model.

Your scenario is here:

  1. Read your entity from Database
  2. Modify properties which you plan to update
  3. Do not modify CreatedBy and CreatedDate
  4. Update your model to Database

Also you model designed not good. You need to use DateTime for dates and Int32 for numbers.

Please change your model like it:

public class MyModelUserProfile
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public int CreatedBy { get; set; }
    public int ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public DateTime CreatedDate { get; set; }
}
Alexander I.
  • 2,380
  • 3
  • 17
  • 42