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

repository InsertAndGetId always returns zero #2570


User avatar
0
carelearning created

Hello,

We have a service that is inserting a record like this:

// GroupAppService
  public GroupDto Add(GroupAddDto dto)
        {
            if (String.IsNullOrWhiteSpace(dto.Name))
                return GroupDto.NullInstance();

	    var group = dto.ToGroup();
            var id = _groupRepository.InsertAndGetId(group);
 
            group.Id = id;//id is always zero

            return dto.FromGroup(group);
        }

We are calling this service from our controller like this:

// GroupController
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(GroupAddViewModel viewModel) 
        { 
            if (!ModelState.IsValid)
                return View(viewModel);
 
	   var dto = new GroupAddDto(viewModel.Name);
            var returnedDto = _groupAppService.Add(dto);
 
            if (returnedDto.Id == 0)
                return Content($"Group with name {returnedDto.Name} returned id is 0.");//this is hit every time and data is inserted
 
            return RedirectToAction("Index");
        }

The information is saved to our table (see attached). But the id returned is always zero. We tried to follow the advice from [https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1387]). Do you know how we can get the ID back of the group inserted?

Thank you for your time and effort.


9 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Which version of ABP do you use on your project ?

  • User Avatar
    0
    carelearning created

    Thanks for the quick reply.

    When we encountered the issue we created a sample project to replicate using the latest version from the dev branch of aspnetzero/aspnet-zero (commit sha1 is bb2c66009d859afc5b466dbee228ba9e32bac9fb).

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you send your project to <a href="mailto:[email protected]">[email protected]</a>, so we can find a faster solution.

    Thanks.

  • User Avatar
    0
    carelearning created

    Hello, Sure as long as your email servers can accept a 20mb zip file or larger. Otherwise we may need somewhere to upload this project for you. Let us know if the email does not arrive. Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We didn't get the email. Maybe you can upload it to google drive and share with <a href="mailto:[email protected]">[email protected]</a> ?

    Thanks a lot.

  • User Avatar
    0
    carelearning created

    We sent FTP credential information to <a href="mailto:[email protected]">[email protected]</a> this morning, Monday, March 6th, 2017 to access the project.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Thank you, I ave downloaded the project, you can remove the ftp credentials if you like. I will get back to you soon.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I have checked your project and it seems like your Group entity implements IEntity. You need to derive it from Entity instead of implementing IEntity. So your final entity should be like

    public class Group : Entity
    {
    ......
    }
    
  • User Avatar
    0
    carelearning created

    Thank you so much. This works!