Search code examples
c#.netaspnetboilerplateef-core-2.2

AppService is inserting 3 rows instead of one


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 false

A false true

A true true

But I don't understand why.

I believe this may have more to do with EF Core, but I am not sure what's wrong here.

I feel like this may have something to do with the navigation properties, I removed them but the outcome remained the same.

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() {
        var res = new ResLinkDto
        {
            TenantId = 1,            
            ModelOneId = 1,            
            ModelTwoId = 1,
            ModelThreeId = 180026       
        };

        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

Solution

  • Sorry for late reply, this was due to AutoMapper magic in my actual application :) This code is working fine as per comments above.

    Thank you all for taking the time to check it out :)