Hello! There are 2 tables related many to many through an intermediate table.

public class Station { [Key] public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public List<RailwayStation> RailwayStations { get; set; } } public class RailwayStation { [Key] public int Id { get; set; } public List<Station> Stations { get; set; } } 

Connection:

  modelBuilder.Entity<Station>() .HasMany(c => c.RailwayStations) .WithMany(p => p.Stations) .Map(m => { m.ToTable("RailwayStationsAllStations"); m.MapLeftKey("StationId"); m.MapRightKey("RailwayStationId"); }); 

Creating a record (passes successfully):

 var stations = _unitOfWork.StationRepository.Get().OrderBy(x=>x.Id).ToList(); _unitOfWork.RailwayStationRepository.Insert(new RailwayStation {Name = "Вокзал 2", AllStations = new List<Station>(stations.Skip(3))}); 

Intermediate table RailwayStationsAllStations is filled with indices correctly.

Attempt to update the station list at the RailwayStation table

  var rs = _unitOfWork.RailwayStationRepository.GetById(1); rs.Stations = new List<Station>(stations.Skip(1).ToList()); rs.Name = "aaa"; _unitOfWork.RailwayStationRepository.Update(rs); await _unitOfWork.SaveAsync(); 

Exception occurs

"System of Data.Data.Entity.Infrastructure.DbUpdateException 'mscorlib.dll but not handled in user code

For your relationships. The EntityEntries Property will not be accepted. It can be made more easily by making it possible. See the InnerException for details. "

I understand it can not find a match for foreign keys. If you comment out

  rs.Stations = new List<Station>(stations.Skip(1).ToList()); 

that

  rs.Name = "aaa"; 

it works.

UnitOfWork and GenericRepository

  public class UnitOfWork : IUnitOfWork { private readonly CisDbContext _context; private GenericRepository<Station> _stationRepository; private GenericRepository<RegulatorySchedule> _regulatoryScheduleRepository; private GenericRepository<OperativeSchedule> _operativeScheduleRepository; private GenericRepository<RailwayStation> _railwayStationRepository; public UnitOfWork(CisDbContext context) { _context = context; } public IRepository<Station> StationRepository => _stationRepository ?? (_stationRepository = new GenericRepository<Station>(_context)); public IRepository<RegulatorySchedule> RegulatoryScheduleRepository => _regulatoryScheduleRepository ?? (_regulatoryScheduleRepository = new GenericRepository<RegulatorySchedule>(_context)); public IRepository<OperativeSchedule> OperativeScheduleRepository => _operativeScheduleRepository ?? (_operativeScheduleRepository = new GenericRepository<OperativeSchedule>(_context)); public IRepository<RailwayStation> RailwayStationRepository => _railwayStationRepository ?? (_railwayStationRepository = new GenericRepository<RailwayStation>(_context)); public async Task<int> SaveAsync() { return await _context.SaveChangesAsync(); } public void Dispose() { _context.Dispose(); } } public class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : class { protected CisDbContext Context { get; } protected DbSet<TEntity> DbSet { get; } public GenericRepository(CisDbContext context) { Context = context; DbSet = context.Set<TEntity>(); } public virtual TEntity GetById(object id) { return DbSet.Find(id); } public virtual async Task<TEntity> GetByIdAsync(int id) { return await DbSet.FindAsync(id); } public virtual IQueryable<TEntity> Get() { return DbSet; } public virtual IQueryable<TEntity> Search(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = Get(); if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split //управление ленивой загрузкой (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } return orderBy?.Invoke(query) ?? query; } public virtual void Insert(TEntity entity) { DbSet.Add(entity); } public virtual void Update(TEntity entity) { DbSet.Attach(entity); Context.Entry(entity).State = EntityState.Modified; } public virtual void Remove(TEntity entity) { if (Context.Entry(entity).State == EntityState.Detached) { DbSet.Attach(entity); } DbSet.Remove(entity); } } 

Please help, I am new to EF.

  • Not enough unitOfWork implementation. - Pavel Mayorov
  • I thought in the tables the problem is somewhere to specify foreign keys - Aldmi
  • I’ll add unitOfWork - Aldmi
  • // deletion was also successful with data deletion from the intermediate table var rs = _unitOfWork.RailwayStationRepository.GetById (2); _unitOfWork.RailwayStationRepository.Remove (rs); await _unitOfWork.SaveAsync (); - Aldmi

0