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

AppService is inserting 3 rows instead of one. #7451


User avatar
0
dexmox created

Issue: AppService is inserting 3 rows instead of one.

What I am trying to achieve is a link table for many to many relations between 3 entities. It appears to be creating one of each type of link between the three entities,

A true fale A false true A true true

But I dont understand why, any assistance would be greatly appreciated and thanks in advance. I believe this may have more to do with EF Core but I am not sure whats wrong here.

public class ModelOne : FullAuditedEntity<long>, IMustHaveTenant
{
        public virtual int TenantId { get; set; }
        public virtual ICollection<ResLink> ResLinks { get; set; }
}
public class ModelTwo : FullAuditedEntity<long>, IMustHaveTenant
{
        public virtual int TenantId { get; set; }
        public virtual ICollection<ResLink> ResLinks { get; set; }
}
public class ModelThree : FullAuditedEntity<long>, IMustHaveTenant
{
        public virtual int TenantId { get; set; }
        public virtual ICollection<ResLink> ResLinks { get; set; }
}
public class ResLink : FullAuditedEntity<long>, IMustHaveTenant
{
        public virtual int TenantId { get; set; }
        
        public virtual long? ModelOneId { get; set; }
        public virtual ModelOne ModelOne { get; set; }
   
        public virtual long? ModelTwoId { get; set; }
        public virtual ModelTwo ModelTwo { get; set; }
        
        public virtual long? ModelThreeId { get; set; }
        public virtual ModelThree ModelThree { get; set; }      
}
public async Task CreateResLink(CreateResInput input) {
    foreach (var resInput in input.ResLinks)
    {    
        var res = new ResLinkDto
        {
            TenantId = AbpSession.TenantId.Value,    
            ModelOneId = resInput.modelOneId,            
            ModelTwoId = resInput.modelTwoId,
            ModelThreeId = resInput.modelThreeId.    
        };

        await _resLinkRepository.InsertAsync(ObjectMapper.Map<ResLink>(res));
    }
}

Insert Result: ID Date User Id M.Date M.User IsDeleted D.Uid D.Time TenantId ModelOneId ModelTwoId ModelThreeId 30050 06/08/2019 13:26:54 6 NULL NULL False NULL NULL 1 1 NULL 180026 30051 06/08/2019 13:26:54 6 NULL NULL False NULL NULL 1 NULL 1 180026 30052 06/08/2019 13:26:54 6 NULL NULL False NULL NULL 1 1 1 180026


4 Answer(s)
  • User Avatar
    0
    musa.demir created

    Are you sure you call the CreateResLink () method only once?

    I don't see any reason for it to record 3 times. I've tried your code just in case. It worked good for me.

  • User Avatar
    0
    dexmox created

    Yep, only being called once via abp.services.app.xxx AppService gets only one object from input, and loops only once and inserts 3.

    Edit -- sorry it is in a for loop and the input is only one object being passed in, will update the original post too.

    foreach (var resInput in input.ResLinks)
    {    
        var res = new ResLinkDto
        {
            TenantId = AbpSession.TenantId.Value,    
            ModelOneId = resInput.modelOneId,            
            ModelTwoId = resInput.modelTwoId,
            ModelThreeId = resInput.modelThreeId.    
        };
        
        await _resLinkRepository.InsertAsync(ObjectMapper.Map<ResLink>(res));
    }
    
  • User Avatar
    0
    musa.demir created

    Are you sure that the loop occurs once? You have multiple row with multiple parameter. In first of them your ModelTwoId is null, in second its ModelOneId is null. It seem like you you call it 3 times with some parameters or your ResLinks has 3 item.

    Can you plase check that.

  • User Avatar
    0
    dexmox created

    @demirmusa - thanks, for your assistance the cause was automapper higher up the chain - I added a collection to a dto earlier, forgot about it and automapper just included it. Thank you again.