我在我的伙伴关系中遇到一个问题。 NET a. 与实体框架核心的核心应用,我一再收到以下答复:
{
"statusCode": 500,
"message": "The instance of entity type Airline cannot be tracked because another instance with the key value {CarrierCode: DY} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached."
}
这发生在我<>PassengerController的“AddBaggage()”方法中的一种前方圆形:await_baggageRepository.AddAsync(NewBaggage);
。 该方法:
[HttpPost("{id}/flight/{flightId}/add-baggage")]
public async Task<ActionResult<Baggage>> AddBaggage(Guid id, int flightId,
[FromBody] List<AddBaggageModel> addBaggageModels)
{
var passenger = await _passengerRepository.GetPassengerByIdAsync(id);
var selectedFlight = await _flightRepository.GetFlightByIdAsync(flightId);
var destination = await _destinationRepository.GetDestinationByCriteriaAsync(f =>
f.IATAAirportCode == addBaggageModels.FirstOrDefault().FinalDestination);
var baggageList = new List<Baggage>();
foreach (var baggageModel in addBaggageModels)
{
var newBaggage = new Baggage
{
PassengerId = passenger.Id,
Weight = baggageModel.Weight,
DestinationId = destination.IATAAirportCode
};
//...some code
else if (baggageModel.TagType == TagTypeEnum.System)
{
var number = _baggageRepository.GetNextSequenceValue("BaggageTagsSequence");
var baggageTag = new BaggageTag(selectedFlight.ScheduledFlight.Airline, number);
newBaggage.BaggageTag = baggageTag;
}
await _baggageRepository.AddAsync(newBaggage); // An exception is thrown here, on the second iteration, when we attempt to assign a reference to the same object/instance of Airline as in the first iteration in the constructor parameter.
baggageList.Add(newBaggage);
var orderedFlights = passenger.Flights
.Where(f => f.Flight.DepartureDateTime >= selectedFlight.DepartureDateTime)
.OrderBy(f => f.Flight.DepartureDateTime)
.ToList();
foreach (var connectingFlight in orderedFlights)
{
//...some code
FlightBaggage flightBaggage = new FlightBaggage
{
Flight = connectingFlight.Flight,
Baggage = newBaggage,
BaggageType = baggageType
};
newBaggage.Flights.Add(flightBaggage);
//...some code
}
}
await _baggageRepository.UpdateAsync(baggageList.ToArray());
return Ok();
}
这里也是BagageTag建筑商之一:
public BaggageTag(Airline? airline, int number)
{
Airline = airline;
LeadingDigit = 0;
Number = number;
TagNumber = _GetBaggageTagNumber();
TagType = TagTypeEnum.System;
}
我在上还有以下法典。 页: 1
public async Task<Flight> GetFlightByIdAsync(int id)
{
var CACHE_KEY = $"Flight_{id}";
if (!_cache.TryGetValue(CACHE_KEY, out Flight flight))
{
flight = await _context.Flights.AsNoTracking()
.Include(_ => _.ScheduledFlight)
.ThenInclude(_ => _.DestinationFrom)
.Include(_ => _.ScheduledFlight)
.ThenInclude(_ => _.DestinationTo)
.Include(_ => _.ScheduledFlight)
.ThenInclude(_ => _.Airline)
.Include(_ => _.Aircraft)
.ThenInclude(_ => _.AircraftType)
.Include(_ => _.Boarding)
.Include(_ => _.ListOfBookedPassengers)
.ThenInclude(_ => _.Passenger)
.Include(_ => _.ListOfCheckedBaggage)
.ThenInclude(_ => _.Baggage)
.Include(_ => _.Seats)
.FirstOrDefaultAsync(_ => _.Id == id);
if (flight != null)
{
_cache.Set(CACHE_KEY, flight, new MemoryCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromMinutes(10)
});
}
}
return flight;
}
public async Task<Passenger> GetPassengerByIdAsync(Guid id)
{
return await _context.Passengers.AsNoTracking()
.Include(_ => _.PNR)
.Include(_ => _.FrequentFlyer)
.Include(_ => _.TravelDocuments)
.Include(_ => _.PassengerCheckedBags)
.ThenInclude(_ => _.BaggageTag)
.Include(_ => _.PassengerCheckedBags)
.ThenInclude(_ => _.SpecialBag)
.Include(_ => _.Flights)
.ThenInclude(_ => _.Flight)
.ThenInclude(_ => _.ScheduledFlight)
.ThenInclude(_ => _.Airline)
.Include(_ => _.Comments)
.ThenInclude(_ => _.PredefinedComment)
.Include(_ => _.AssignedSeats)
.Include(_ => _.SpecialServiceRequests)
.ThenInclude(_ => _.SSRCode)
.FirstOrDefaultAsync(_ => _.Id == id);
}
我的理解是,问题源于“变特拉克”试图吞并一个已经在此背景下追踪的实体。 然而,我不知道如何有效地避免这种情况。 I ve Read about detaching entities using dbContext.Entry(entity). 国家=实体国家。
我是否应在旅客存放处、飞行存放处或通用保存处等方法中加入,如AddAsync()、更新Async()和删除Async()? 或者,我是否在我的评议书中推翻了“拯救儿童协会”的方法,并将其列入?
我赞赏任何有效解决这一问题的指导或替代解决办法。 感谢你的帮助!
我试图从我的储存库中删除所有“NoTrack()”和“As Queryable()”方法,所有方法都非常有效。 然而,我最初增加了这些方法,以降低记忆的间接费用,从那时起,我一直面临这一问题。