Base solution for your next web application
Open Closed

CreationTime, CreatorUserIdgetting set on Update API call? #4161


User avatar
0
manojreddy created

I am making an update API Call, but its updating CreationTime and CreatorUserId columns even If I'm not passing these values.

{
  "testCode": "string",
  "testName": "string",
  "testDesc": "string",
  "id": 1
}
public async Task UpdateTest(TestDetailsDTO input)
        {
            var classobj = ObjectMapper.Map<Test>(input);
            await UpdateAsync(classobj);
        }
public class TaxJurisdictionDetailsDTO : FullAuditedEntityDto
    {
        public string JurisdictionCode { get; set; }
        public string JurisdictionName { get; set; }
        public string JurisdictionDesc { get; set; }
    }

Parameter input gets CreationTime in UpdateTest service method.


10 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi @ManojReddy,

    if you use mapper like following, a new object is creating. So all of properties are changing.

    var classobj = ObjectMapper.Map<Test>(input);
    

    if you want to update entity, first you get the entity from database, and map it from dto like following.

    var classobj = _someService.GetEntity(input.id);
    ObjectMapper.Map(input, classobj );
    
  • User Avatar
    0
    manojreddy created

    Thanks for your response.

    But in this case it will make extra DB call by calling GetEntity method. Don't we have any alternative approach?

  • User Avatar
    0
    alirizaadiyahsi created

    When you select entity from DB, EF can track this entity, if you want to do this manually, you should attach the entity to dbContext. For examlple;

    // Create new stub with correct id and attach to context.
    var entity = new myEntity { Id = input.Id };
    db.SomeEntities.Attach(entity);
    
    // Now the entity is being tracked by EF, update required properties.
    entity.Title = "new title";
    entity.Url = "new-url";
    		
    // EF knows only to update the propeties specified above.
    db.SaveChanges();
    

    To do this in aspnet zero, you should change repositorybase or you can write a custom repository.

  • User Avatar
    0
    manojreddy created

    Thanks, Could you please provide sample declaration for repositorybase or custom repository.

  • User Avatar
    0
    alirizaadiyahsi created

    Check this for a starting point: <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/EntityFramework-Integration#DocCustomRepositoryMethods">https://aspnetboilerplate.com/Pages/Doc ... oryMethods</a>

  • User Avatar
    0
    manojreddy created

    What are pros and cons of attaching the entity to dbContext?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    What are pros and cons of attaching the entity to dbContext?

    This is an Entity Framework related topic. You can find best answers on the web.

  • User Avatar
    0
    manojreddy created

    Following code is not working, Its updating CreationTime, CreatorUserId.

    var classobj = _someService.GetEntity(input.id);
    ObjectMapper.Map(input, classobj );
    
  • User Avatar
    0
    manojreddy created

    <cite>ManojReddy: </cite> I am making an update API Call, but its updating CreationTime and CreatorUserId columns even If I'm not passing these values.

    {
     "testCode": "string",
     "testName": "string",
     "testDesc": "string",
     "id": 1
    }
    
    public async Task UpdateTest(TestDetailsDTO input)
           {
               var classobj = ObjectMapper.Map<Test>(input);
               await UpdateAsync(classobj);
           }
    
    public class TaxJurisdictionDetailsDTO : FullAuditedEntityDto
       {
           public string CodeCode { get; set; }
           public string CodeName { get; set; }
           public string CodeDesc { get; set; }
       }
    

    Parameter input gets CreationTime in UpdateTest service method.

  • User Avatar
    0
    aaron created
    Support Team

    Remove CreationTime and CreatorUserId from TestDetailsDto.

    If you want a workaround:

    var classobj = _someService.GetEntity(input.id);
    input.CreationTime = classobj.CreationTime;
    input.CreatorUserId = classobj.CreatorUserId;
    ObjectMapper.Map(input, classobj);