Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

RAD Tool Update confusion #4828


User avatar
0
BobIngham created

I have been blindly following standards laid out from code generated by the RAD tool (which is such a time saver, thanks guys) when I noticed the following method:

[AbpAuthorize(AppPermissions.Pages_Test_Edit)]
private async Task Update(CreateOrEditTestDto input)
{
    var test = await _testRepository.FirstOrDefaultAsync((int)input.Id);
    ObjectMapper.Map(input, test);
}

This works but I am little confused as to where the update method in the repository is called, I was expecting to see something like:

[AbpAuthorize(AppPermissions.Pages_Test_Edit)]
private async Task Update(CreateOrEditTestDto input)
{
    var test = await _testRepository.FirstOrDefaultAsync((int)input.Id);
...
    await _testRepository.UpdateAsync(ObjectMapper.Map(input, test));
}

What is the magic with the generated code from the RAD tool? Where is the update called? Can someone point me to some documentation here because this surprised me!


10 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    The magic is change tracking:

    Calling Update is unnecessary if the entity is already tracked. In this case, it is tracked since it is retrieved from the repository. For example, another way is to map to a new instance (to replace an entity entirely). In that case, you will have to call Update.

  • User Avatar
    0
    faisalalam created

    what is the update on RAD tool new version? You guys promised two weeks back support with foreign key

  • User Avatar
    0
    alper created
    Support Team

    hi, navigation property (foreign key support) feature is completed. waiting for other features of AspNet Zero to be completed.

  • User Avatar
    0
    faisalalam created

    <cite>alper: </cite> hi, navigation property (foreign key support) feature is completed. waiting for other features of AspNet Zero to be completed.

    :lol:

  • User Avatar
    0
    BobIngham created

    Hi Aaron, I'm just going to have to go along with you on this one. I've been working with EF for years now (though never with Core before Zero) and always called an update for an update. Even the documentation you referred to explicitly states:

    var db = new TestContext();
    var book = new Book { BookId = 1 }; 
    db.Update(book); // Book Modified
    

    I don't see how;

    ObjectMapper.Map(input, test);
    

    equates to the same thing. The older I get the less I know and the more I realise I have to learn! I will just have to accept that the rabbit came out of the hat even though I know it's impossible....

  • User Avatar
    0
    yekalkan created

    It is explained in Unit Of Work documentation.

    <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#automatically-saving-changes">https://aspnetboilerplate.com/Pages/Doc ... ng-changes</a>

  • User Avatar
    0
    BobIngham created

    @yekalkan - thanks, I read that too. But I'm still not sure how you pulled all those birds from your hankerchief. It must be magic.... :oops:

  • User Avatar
    0
    faisalalam created

    Can I download the updated version today?

  • User Avatar
    0
    aaron created
    Support Team

    <cite>BobIngham: </cite> Even the documentation you referred to explicitly states:

    var db = new TestContext();
    var book = new Book { BookId = 1 }; 
    db.Update(book); // Book Modified
    

    That's a case of "For example, another way is to map to a new instance (to replace an entity entirely). In that case, you will have to call Update."

    <cite>BobIngham: </cite> I don't see how

    ObjectMapper.Map(input, test);
    

    equates to the same thing.

    It doesn't. _testRepository.FirstOrDefaultAsync retrieves a tracked entity, new Book creates an untracked entity.

  • User Avatar
    0
    aaron created
    Support Team

    <cite>faisalalam: </cite> Can I download the updated version today?

    You can track progress of v5.3 milestone to get an idea of when it will be released.