Base solution for your next web application
Open Closed

Repository.Insert does not work #95


User avatar
0
lcyhjx created

Hi,

I define a entiy named Order, and define a service In application layer called orderService. In orderService, there are two operations. GetAll() and CreateNewOrder();

The GetAll() operation works fine, the mainly code is

var orders= _orderRepository.GetAll().ToList();

but the CreateNewOrder does not work, I debug it, the following statement has been executed.

var newOrder = _orderRepository.Insert(order);

But nothing happened, the new order is not inserted in to DB, no exception.

I am sure something is wrong in my coding, but no exception when execute Repository.Insert statement, so it is hardly for me to know what’s the issue.

Do you have any suggestion?


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

    Are you sure that it's not inserted to database? It's not immediately inserted, it's inserted at end of the unit of work (when your application service method ends) if there is no exception. You can see logs if there is an exception. If you want that it immediately inserts to db, call CurrentUnitOfWork.SaveChanges method.

  • User Avatar
    0
    lcyhjx created

    it's not inserted to database, the service operation is executed completed, but still cannot find it in DB. Also, I tried to make following change - I did not pass the value to a un-nullable field, I would like to get a exception to verify the Repository.Insert method, but unfortunattly, still no error, no exception thrown.

  • User Avatar
    0
    hikalkan created
    Support Team

    OK, probably you're getting an exception. Normally, all exceptions are logged to Logs folder of the web project. If not, you can try to listen ABP exceptions. See this topic: #87

  • User Avatar
    0
    lcyhjx created

    Thanks! if I invoke unitOfWorkManager.Current.SaveChanges() explictly, then the data is inserted into DB. But I cannot understand, why I need to invoke unitOfWorkManager.Current.SaveChanges() explictly. as my understand, the transanction should be commited automatically at the end of method.

  • User Avatar
    0
    hikalkan created
    Support Team

    It's very interesting. Because, we do it every day and not calling SaveChanges. I could not understand the problem. Can you share app service completely? Also, did you check if there is an error in log or in the event?

  • User Avatar
    0
    lcyhjx created

    Application Service

    public class OrderPublishService : ApplicationService, IOrderPublishService
        {
    
            private readonly IRepository<Order,long> _orderRepository;
            private readonly OrderDomainService _orderDomainService;
            private readonly IUnitOfWorkManager _unitOfWorkManager;
    
    
            public OrderPublishService(IRepository<Order, long> orderRepository,OrderDomainService orderDomainService , IUnitOfWorkManager unitOfWorkManager)
            {
                _orderRepository = orderRepository;
                _orderDomainService = orderDomainService;
                _unitOfWorkManager = unitOfWorkManager;
            }
    
         
            public  PublishOrderOutput PublishOrder(PublishOrderInput input)
            {
                //map, I do not user automap here
                Order order = input.Map2Order(); 
    
               _orderRepository.Insert(order);
       
                _unitOfWorkManager.Current.SaveChanges();
    
                return new PublishOrderOutput()
                {
                     
                };
            }
        }
    

    //Repository

    public class OrderRepository : BOMRepositoryBase<Order, long>,IOrderRepository
        {
            public OrderRepository(IDbContextProvider<BOMDbContext> dbContextProvider)
                : base(dbContextProvider)
            {
            }
        }
    

    //Domain Entity

    public class Order : FullAuditedEntity<long, User>  
        {
    
            public string OrderNumber { get; set; }
    
           //other property
           .....
    }