Remove unnecessary branching around EF logic

This commit is contained in:
Dean Herbert
2021-10-22 14:48:20 +09:00
parent 93482414d6
commit eef9949a0a

View File

@ -511,29 +511,21 @@ namespace osu.Game.Database
/// <param name="file">The existing file to be deleted.</param> /// <param name="file">The existing file to be deleted.</param>
public void DeleteFile(TModel model, TFileModel file) public void DeleteFile(TModel model, TFileModel file)
{ {
if (model.ID > 0) using (var usage = ContextFactory.GetForWrite())
{ {
using (var usage = ContextFactory.GetForWrite()) // Dereference the existing file info, since the file model will be removed.
if (file.FileInfo != null)
{ {
// Dereference the existing file info, since the file model will be removed. Files.Dereference(file.FileInfo);
if (file.FileInfo != null)
if (file.ID > 0)
{ {
Files.Dereference(file.FileInfo); // This shouldn't be required, but here for safety in case the provided TModel is not being change tracked
// Definitely can be removed once we rework the database backend.
if (file.ID > 0) usage.Context.Set<TFileModel>().Remove(file);
{
// This shouldn't be required, but here for safety in case the provided TModel is not being change tracked
// Definitely can be removed once we rework the database backend.
usage.Context.Set<TFileModel>().Remove(file);
}
} }
model.Files.Remove(file);
} }
}
else
{
Files.Dereference(file.FileInfo);
model.Files.Remove(file); model.Files.Remove(file);
} }
} }
@ -546,29 +538,17 @@ namespace osu.Game.Database
/// <param name="filename">The filename for the new file.</param> /// <param name="filename">The filename for the new file.</param>
public void AddFile(TModel model, Stream contents, string filename) public void AddFile(TModel model, Stream contents, string filename)
{ {
if (model.ID > 0) using (ContextFactory.GetForWrite())
{ {
using (ContextFactory.GetForWrite())
{
model.Files.Add(new TFileModel
{
Filename = filename,
FileInfo = Files.Add(contents)
});
Update(model);
}
}
else
{
// This function may be called during the import process.
// Should not exist like this once we have switched to realm.
model.Files.Add(new TFileModel model.Files.Add(new TFileModel
{ {
Filename = filename, Filename = filename,
FileInfo = Files.Add(contents) FileInfo = Files.Add(contents)
}); });
} }
if (model.ID > 0)
Update(model);
} }
/// <summary> /// <summary>