Open Closed

Linq Include on deleted rows #2892


0
ricavir created

Hello,

While implementing my entities on my asp.net zero project (angular 2), I'm facing following issue. I have two entities AccountingCode and VatCode. VatCode has a reference to AccountingCode (I took your step-by-step document has an example)

When I try the following applicationservice method, it works great :

public async Task<PagedResultDto<VatCodeListDto>> GetVatCodes(GetVatCodesInput input)
        {
            var vatCodes = _vatCodeRepository
                .GetAll()
                .Include(p=>p.AccountingCode)     
                .WhereIf(
                    !input.Filter.IsNullOrEmpty(),
                    p => p.Description.Contains(input.Filter) ||
                         p.Code.Contains(input.Filter)
                );

            var resultCount = await vatCodes.CountAsync();
            var results = await vatCodes
                .OrderBy(input.Sorting)
                .PageBy(input)
                .ToListAsync();

            var eventTypeListDtos = results.MapTo<List<VatCodeListDto>>();

            return new PagedResultDto<VatCodeListDto>(resultCount, eventTypeListDtos);

        }

AccountingCode entity is added to the result without lazy loading by adding the line : .Include(p=>p.AccountingCode)

The problem is when I delete an element from AcountingCode table that was linked to a VatCode element. In that case, the previous query removes all vatcode elements that where linked to the deleted AccountingCode element => but they are still in the database.

If I remove the line .Include(p=>p.AccountingCode), it works fine : all VatCode elements are sent to the client (with no AccountingCode linked to it).

Do you know how to proceed in order to keep Include method in the query (and avoid lazy loading) ?


1 Answer(s)
  • 0
    ismcagdas created
    Support Team

    Hi,

    I think .Include(p=>p.AccountingCode) uses an inner join. You can change your query to use a left join linq syntax, see <a class="postlink" href="https://msdn.microsoft.com/tr-tr/vstudio/ee908647.aspx#leftouterjoin">https://msdn.microsoft.com/tr-tr/vstudi ... touterjoin</a>.

    Please let us know if it does not work.

    Thanks.