标签 标签 标签

  • 一句话的事儿:

场景一:查询Clubs以及关联的League

  1. //查询Clubs以及关联的League
  2. var club = context.Clubs.Include(x => x.League).FirstOrDefault();
  3. club.League.Name += "@";
  4. context.SaveChanges();

场景二:考虑离线状态

  1. //context与newContext是离线状态
  2. var game = context.Games
  3. .Include(x => x.GamePlayers)
  4. .ThenInclude(x => x.Player)
  5. .FirstOrDefault();
  6. var firstPlayer = game.GamePlayers[0].Player;
  7. firstPlayer.Name += "$";
  8. {
  9. using var newContext = new DataContext();
  10. newContext.Players.Update(firstPlayer);
  11. newContext.SaveChanges();
  12. }

image.png
减少Update次数

  1. newContext.Entry(firstPlayer).State = EntityState.Modified;

image.png

场景三:如何设置多对多的关系

通过已知的Game与Player创建GamePlayer

  1. var gamePlayer = new GamePlayer
  2. {
  3. GameId = 1,
  4. PlayerId = 5
  5. };
  6. context.Add(gamePlayer);
  7. context.SaveChanges();

image.png
game通过查询,player通过命名

  1. var game = context.Games.First();
  2. game.GamePlayers.Add(new GamePlayer { PlayerId = 4 });
  3. context.SaveChanges();

image.png

场景四:删除多对多关系->删除GamePlayer

  1. //先将GamePlayer查询出来
  2. //也可以new一个出来
  3. var gamePlayer = new GamePlayer { GameId = 1, PlayerId = 5 };
  4. context.Remove(gamePlayer);
  5. context.SaveChanges();

image.png

场景五:更新多对多的关系

//01.首先解除二者之间的关系,然后重新创建关系

场景六:修改一对一的关系

  1. //修改一对一的关系
  2. var player = context.Players.First();
  3. player.Resume = new Resume { Description = "Update Description!!!" };
  4. context.SaveChanges();

image.png
主键为空:为唯一索引

场景七:修改一对一(离线状态)

  1. var player = context.Players
  2. .AsNoTracking()
  3. .OrderBy(x => x.Id)
  4. .Last();
  5. player.Resume = new Resume { Description = "update description in offline." };
  6. {
  7. using var newContext = new DataContext();
  8. newContext.Attach(player);
  9. newContext.SaveChanges();
  10. }

image.png
接下来我们做一个事情,上面的resume我们已经添加了,那么再添加一次会发生什么呢? :::warning Microsoft.EntityFrameworkCore.DbUpdateException
HResult=0x80131500 ::: :::warning Message=An error occurred while updating the entries. See the inner exception for details. ::: :::warning Source=Microsoft.EntityFrameworkCore.Relational ::: :::warning StackTrace: ::: :::warning at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection) ::: :::warning at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection) ::: :::warning at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList1 entries) ::: :::warning at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList1 entriesToSave) ::: :::warning at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess) ::: :::warning at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func`3 verifySucceeded) ::: :::warning at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess) ::: :::warning at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) ::: :::warning at Microsoft.EntityFrameworkCore.DbContext.SaveChanges() ::: :::warning at Demo.App.Program.Main(String[] args) in D:\Projects\TutorialOnline\Entity Framework Core 3.1 极简入门教程\EFCoreDemo3.x\Demo.App\Program.cs:line 301 ::: :::warning This exception was originally thrown at this call stack: ::: :::warning [External Code] ::: :::warning Inner Exception 1: ::: :::warning SqlException: Cannot insert duplicate key row in object ‘dbo.Resumes’ with unique index ‘IX_Resumes_PlayerId’. The duplicate key value is (7). ::: :::warning The statement has been terminated. ::: 这是因为PlayerId是唯一索引导致的
image.png

  1. var player = context.Players
  2. .Include(x => x.Resume)
  3. .OrderBy(x => x.Id)
  4. .Last();
  5. player.Resume = new Resume { Description = "update description in offline." };
  6. context.SaveChanges();

  • 本文作者:GeekPower - Felix Sun
  • 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!