标签 标签 标签
- 一句话的事儿:
场景一:查询Clubs以及关联的League
//查询Clubs以及关联的League
var club = context.Clubs.Include(x => x.League).FirstOrDefault();
club.League.Name += "@";
context.SaveChanges();
场景二:考虑离线状态
//context与newContext是离线状态
var game = context.Games
.Include(x => x.GamePlayers)
.ThenInclude(x => x.Player)
.FirstOrDefault();
var firstPlayer = game.GamePlayers[0].Player;
firstPlayer.Name += "$";
{
using var newContext = new DataContext();
newContext.Players.Update(firstPlayer);
newContext.SaveChanges();
}
减少Update次数
newContext.Entry(firstPlayer).State = EntityState.Modified;
场景三:如何设置多对多的关系
通过已知的Game与Player创建GamePlayer
var gamePlayer = new GamePlayer
{
GameId = 1,
PlayerId = 5
};
context.Add(gamePlayer);
context.SaveChanges();
game通过查询,player通过命名
var game = context.Games.First();
game.GamePlayers.Add(new GamePlayer { PlayerId = 4 });
context.SaveChanges();
场景四:删除多对多关系->删除GamePlayer
//先将GamePlayer查询出来
//也可以new一个出来
var gamePlayer = new GamePlayer { GameId = 1, PlayerId = 5 };
context.Remove(gamePlayer);
context.SaveChanges();
场景五:更新多对多的关系
场景六:修改一对一的关系
//修改一对一的关系
var player = context.Players.First();
player.Resume = new Resume { Description = "Update Description!!!" };
context.SaveChanges();
场景七:修改一对一(离线状态)
var player = context.Players
.AsNoTracking()
.OrderBy(x => x.Id)
.Last();
player.Resume = new Resume { Description = "update description in offline." };
{
using var newContext = new DataContext();
newContext.Attach(player);
newContext.SaveChanges();
}
接下来我们做一个事情,上面的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(IList
1 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, Func
3 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是唯一索引导致的
var player = context.Players
.Include(x => x.Resume)
.OrderBy(x => x.Id)
.Last();
player.Resume = new Resume { Description = "update description in offline." };
context.SaveChanges();
- 本文作者:GeekPower - Felix Sun
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!