mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 15:07:44 +08:00
Extract requerying of navigational properties from DbContext
This commit is contained in:
parent
a60ff80c04
commit
0a1e325fc7
@ -174,21 +174,7 @@ namespace osu.Game.Beatmaps
|
|||||||
if (beatmapSet.Beatmaps.Any(b => b.BaseDifficulty == null))
|
if (beatmapSet.Beatmaps.Any(b => b.BaseDifficulty == null))
|
||||||
throw new InvalidOperationException($"Cannot import {nameof(BeatmapInfo)} with null {nameof(BeatmapInfo.BaseDifficulty)}.");
|
throw new InvalidOperationException($"Cannot import {nameof(BeatmapInfo)} with null {nameof(BeatmapInfo.BaseDifficulty)}.");
|
||||||
|
|
||||||
var dbContext = ContextFactory.Get();
|
beatmapSet.Requery(ContextFactory);
|
||||||
|
|
||||||
// Workaround System.InvalidOperationException
|
|
||||||
// The instance of entity type 'RulesetInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
|
|
||||||
foreach (var beatmap in beatmapSet.Beatmaps)
|
|
||||||
{
|
|
||||||
beatmap.Ruleset = dbContext.RulesetInfo.Find(beatmap.RulesetID);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Workaround System.InvalidOperationException
|
|
||||||
// The instance of entity type 'FileInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
|
|
||||||
foreach (var file in beatmapSet.Files)
|
|
||||||
{
|
|
||||||
file.FileInfo = dbContext.FileInfo.Find(file.FileInfoID);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if a set already exists with the same online id, delete if it does.
|
// check if a set already exists with the same online id, delete if it does.
|
||||||
if (beatmapSet.OnlineBeatmapSetID != null)
|
if (beatmapSet.OnlineBeatmapSetID != null)
|
||||||
|
44
osu.Game/Database/Extensions.cs
Normal file
44
osu.Game/Database/Extensions.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using osu.Game.Beatmaps;
|
||||||
|
using osu.Game.Scoring;
|
||||||
|
|
||||||
|
namespace osu.Game.Database
|
||||||
|
{
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
public static void Requery(this BeatmapSetInfo beatmapSetInfo, IDatabaseContextFactory databaseContextFactory)
|
||||||
|
{
|
||||||
|
var dbContext = databaseContextFactory.Get();
|
||||||
|
|
||||||
|
foreach (var beatmap in beatmapSetInfo.Beatmaps)
|
||||||
|
{
|
||||||
|
// Workaround System.InvalidOperationException
|
||||||
|
// The instance of entity type 'RulesetInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
|
||||||
|
beatmap.Ruleset = dbContext.RulesetInfo.Find(beatmap.RulesetID);
|
||||||
|
}
|
||||||
|
|
||||||
|
beatmapSetInfo.Files.Requery(databaseContextFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Requery(this ScoreInfo scoreInfo, IDatabaseContextFactory databaseContextFactory)
|
||||||
|
{
|
||||||
|
scoreInfo.Files.Requery(databaseContextFactory);
|
||||||
|
scoreInfo.Beatmap.BeatmapSet.Files.Requery(databaseContextFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Requery<T>(this List<T> files, IDatabaseContextFactory databaseContextFactory) where T : class, INamedFileInfo
|
||||||
|
{
|
||||||
|
var dbContext = databaseContextFactory.Get();
|
||||||
|
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
// Workaround System.InvalidOperationException
|
||||||
|
// The instance of entity type 'FileInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
|
||||||
|
file.FileInfo = dbContext.FileInfo.Find(file.FileInfoID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -54,19 +54,7 @@ namespace osu.Game.Scoring
|
|||||||
|
|
||||||
protected override void PreImport(ScoreInfo model)
|
protected override void PreImport(ScoreInfo model)
|
||||||
{
|
{
|
||||||
var dbContext = ContextFactory.Get();
|
model.Requery(ContextFactory);
|
||||||
|
|
||||||
// Workaround System.InvalidOperationException
|
|
||||||
// The instance of entity type 'FileInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
|
|
||||||
foreach (var file in model.Files)
|
|
||||||
{
|
|
||||||
file.FileInfo = dbContext.FileInfo.Find(file.FileInfoID);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var file in model.Beatmap.BeatmapSet.Files)
|
|
||||||
{
|
|
||||||
file.FileInfo = dbContext.FileInfo.Find(file.FileInfoID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override ScoreInfo CreateModel(ArchiveReader archive)
|
protected override ScoreInfo CreateModel(ArchiveReader archive)
|
||||||
|
@ -144,14 +144,7 @@ namespace osu.Game.Skinning
|
|||||||
|
|
||||||
protected override void PreImport(SkinInfo model)
|
protected override void PreImport(SkinInfo model)
|
||||||
{
|
{
|
||||||
var dbContext = ContextFactory.Get();
|
model.Files.Requery(ContextFactory);
|
||||||
|
|
||||||
// Workaround System.InvalidOperationException
|
|
||||||
// The instance of entity type 'FileInfo' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
|
|
||||||
foreach (var file in model.Files)
|
|
||||||
{
|
|
||||||
file.FileInfo = dbContext.FileInfo.Find(file.FileInfoID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user