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

Module Zero SaveChanges? #262


User avatar
0
drsmallbone created

First of all cok tesekkurler!

Boilerplate and Module Zero are amazing. I have been playing with them for over 6 months and they are brilliant, also documentation is spot on.

However, I am stuck on what is probably a straight forward problem.

In my code I am currently creating a new Role, which I need to be added to the database so I can get the Id to add permissions. Usually I would have done context.SaveChanges()

if (role.Id == 0)
            {
                IdentityResult result = await _roleManager.CheckDuplicateRoleNameAsync(null, role.Name, role.DisplayName);
                if (!result.Succeeded)
                {
                    throw new UserFriendlyException(result.Errors.FirstOrDefault().ToString());
                }
                else
                {
                    IdentityResult createRole = await _roleManager.CreateAsync(role);
                    if (!createRole.Succeeded)
                    {
                        throw new UserFriendlyException("Something went wrong, please try again");
                    }
                    
                    // SAVE TO DATABASE
                    // GET ID OF NEW ROLE
                    // DO SOMETHING
                }
            }

Any help would be really appreciated.


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Tesekkürler :)

    It's simple, just inject IUnitOfWormManager and call IUnitOfWormManager.Current.SaveChanges(). It saves entity to database and fills Id property. Then you can reach to role.Id, you will see that it's set :) If this is an app service, just call CurrentUnitOfWork.SaveChanges();

    For document: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocUowSaveChanges">http://www.aspnetboilerplate.com/Pages/ ... aveChanges</a>

    Have a nice day.

  • User Avatar
    0
    drsmallbone created

    Thanks for getting back so quickly.

    I guess you mean IUnitOfWorkManager (in case anyone copy and pastes)

    I knew it had to be UnitOfWork but I completely missed the current section. Can not believe I missed it in the documents.

    Thanks once again!

    Next time I am in Istanbul the beers are on me (I married a Turkish woman so will be sooner rather than later)

    Danny