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)
-
0
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 );
-
0
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?
-
0
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.
-
0
Thanks, Could you please provide sample declaration for repositorybase or custom repository.
-
0
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>
-
0
What are pros and cons of attaching the entity to dbContext?
-
0
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.
-
0
Following code is not working, Its updating CreationTime, CreatorUserId.
var classobj = _someService.GetEntity(input.id); ObjectMapper.Map(input, classobj );
-
0
<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.
-
0
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);