Вставка и обновление ядра структуры многих для многих - PullRequest
0 голосов
/ 28 февраля 2020

Итак, позвольте мне объяснить. У меня есть 2 таблицы: путешествие и адрес. Отношение многие ко многим, с дополнительной таблицей под названием JourneyAddress, которая содержит все записи для идентификаторов, как обычные многие ко многим. Но я не понимаю, как обновить эту таблицу новыми адресами, каждый раз, когда я пытаюсь обновить ее, просто выкидываю исключение ниже. Я попытался удалить всю старую запись с идентификатором поездки - идентификатором адреса, а затем вставить новые, но это тоже не сработало, я полагаю, это неправильный способ обработки многих для многих операций вставки и обновления.

Класс путешествий:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace ApplicazioneAutotrasporti.Model
{
    public class Journey
    {
        [Key]
        public int id { get; set; }
        public virtual ICollection<JourneyAddress> journeyAddress { get; set; }
        [ForeignKey("vehicleId")]
        public Vehicle vehicle { get; set; }
        public int vehicleId { get; set; }
        [ForeignKey("driverId")]
        public Driver driver { get; set; }
        public int driverId { get; set; }
        public DateTime date { get; set; }
        public string measureUnit { get; set; }
        public float quantity { get; set; }
        public float valueForUnit { get; set; }
        public float timeStop { get; set; }
        public float valueStopTime { get; set; }
        public string customer { get; set; }
    }
}

Класс адресов:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace ApplicazioneAutotrasporti.Model
{
    public class Address
    {
        [Key]
        public int id { get; set; }
        [Required]
        public string name { get; set; }
        public virtual ICollection<JourneyAddress> JourneyAddress { get; set; }

    }
}

Контекст базы данных:

using Microsoft.EntityFrameworkCore;
using ApplicazioneAutotrasporti.Model;

namespace TranscoopTrips.Data
{
    public class DatabaseContext : DbContext
    {
        public DatabaseContext (DbContextOptions<DatabaseContext> options)
            : base(options)
        {
        }

        public DbSet<Driver> Driver { get; set; }
        public DbSet<Vehicle> Vehicle { get; set; }
        public DbSet<Address> Address { get; set; }
        public DbSet<Fuel> Fuel { get; set; }
        public DbSet<Journey> Journey { get; set; }
        public DbSet<Maintenance> Maintenance { get; set; }
        public DbSet<JourneyAddress> JourneyAddress { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<JourneyAddress>()
                .HasKey(bc => new { bc.journeyId, bc.addressId });
            modelBuilder.Entity<JourneyAddress>()
                .HasOne(bc => bc.journey)
                .WithMany(b => b.journeyAddress)
                .HasForeignKey(bc => bc.journeyId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<JourneyAddress>()
                .HasOne(bc => bc.address)
                .WithMany(c => c.JourneyAddress)
                .HasForeignKey(bc => bc.addressId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<Fuel>()
                .HasOne(c => c.vehicle)
                .WithMany(e => e.Fuels)
                .HasForeignKey(c => c.vehicleId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<Journey>()
                .HasOne(c => c.vehicle)
                .WithMany(e => e.Journeys)
                .HasForeignKey(c => c.vehicleId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<Journey>()
                .HasOne(c => c.driver)
                .WithMany(e => e.Journeys)
                .HasForeignKey(c => c.driverId)
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<Maintenance>()
                .HasOne(c => c.vehicle)
                .WithMany(e => e.Maintenances)
                .HasForeignKey(c => c.vehicleId)
                .OnDelete(DeleteBehavior.Cascade);
        }
    }
}

Вот метод "PutJourney" из JourneysController:

public async Task<IActionResult> PutJourney(int id, Journey journey)
    {
        if (id != journey.id)
        {
            return BadRequest();
        }

        _context.Entry(journey).State = EntityState.Modified;

        try
        {
            var journeyAddress = _context.JourneyAddress.Where(p => p.journeyId == id);
            _context.JourneyAddress.RemoveRange(journeyAddress);
            _context.JourneyAddress.AddRange(journey.journeyAddress);
            _repo.Update(journey);
            var save = await _repo.SaveAsync(journey);
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!JourneyExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return NoContent();
    }

Вот исключение:

System.InvalidOperationException: The instance of entity type 'JourneyAddress' cannot be tracked because another instance with the same key value for {'journeyId', 'addressId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.ThrowIdentityConflict(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
   at Microsoft.EntityFrameworkCore.DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState)
   at Microsoft.EntityFrameworkCore.DbContext.AddRange(IEnumerable`1 entities)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.AddRange(IEnumerable`1 entities)
   at TranscoopTrips.Controllers.JourneysController.PutJourney(Int32 id, Journey journey) in D:\Davide\Progetti\In corso\TranscoopTrips\TranscoopTrips\Controllers\JourneysController.cs:line 86
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Ответы [ 2 ]

1 голос
/ 28 февраля 2020

Сначала вставьте. Попробуйте это.

var journeyAddress = _context.JourneyAddress.Where(p => p.journeyId == id);
_repo.Update(journey);
_context.JourneyAddress.RemoveRange(journeyAddress);
_context.SaveChanges();

_repo.Update добавит также trip.journeyAddress. Поэтому вам не нужно добавлять снова.

РЕДАКТИРОВАНИЕ.

Пользователь = Путешествие

User.UserCourse = Journey.JourneyAddress

UserCourse = JourneyAddress

enter image description here

До вызова функции,

enter image description here

После вызова функции,

enter image description here

0 голосов
/ 28 февраля 2020

Итак, вы загрузили journey.journeyAddress из базы данных, добавили / удалили записи, затем хотите сохранить изменения?

Лично я бы заставил клиента отслеживать новые / удаленные записи, затем только Добавить / Удалить те.

Вы можете загрузить существующие JourneyAddress записи из базы данных .AsNoTracking(), сравнить список с новым journey.journeyAddress, затем только добавить / удалить записи, которых нет в обоих списках.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...