The instance of entity type ‘XXXX’ cannot be tracked because another instance with the same key value for {‘XXXX’} is already being tracked.


While working my EF core Application I was getting this exception message. I fixed this issue like this

Step 1: In Insert/update method make ensure to detach the entity.

_db.Entry(localRopFiles).State = EntityState.Detached;

Step 2: If you are loading data on page load then use AsNoTracking() method

var query = await _db.LocalRopFiles.AsNoTracking().ToListAsync();

I hope this will help some one who is getting this type of exception.

How to call token based web api in Blazor


Here is the syntax for calling taken based web api in Blazor server or webassembly.

using MyFleetApp.Data.Model;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;


namespace MyFleetApp.Data.Service
{
    public class InvoiceSearchService : IInvoiceSerach
    {
        private readonly HttpClient httpClient;
        public InvoiceSearchService(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }
       

        public async Task<Rootobject> GetInvoices(Rootobject objParameter)
        {
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", objParameter.token);
            var result = await httpClient.PostAsJsonAsync<Rootobject>("Web API URL will be here", objParameter);
            Rootobject objInvoice = new Rootobject();
            if (result.IsSuccessStatusCode)
            {
                objInvoice = await result.Content.ReadFromJsonAsync<Rootobject>();
            }

            return objInvoice;
        }
    }
}