-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
692ad46
commit 382b968
Showing
7 changed files
with
337 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
// Copyright 2022 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license, please see LICENSE.md in the project root for license information or contact [email protected] for full terms. | ||
|
||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure.Internal; | ||
using Cite.Api.Data.Models; | ||
using Cite.Api.Data.Extensions; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal; | ||
|
||
namespace Cite.Api.Data | ||
{ | ||
public class CiteContext : DbContext | ||
{ | ||
private DbContextOptions<CiteContext> _options; | ||
// Needed for EventInterceptor | ||
public IServiceProvider ServiceProvider; | ||
|
||
public CiteContext(DbContextOptions<CiteContext> options) : base(options) { | ||
_options = options; | ||
} | ||
public CiteContext(DbContextOptions<CiteContext> options) : base(options) { } | ||
|
||
public DbSet<ScoringModelEntity> ScoringModels { get; set; } | ||
public DbSet<ScoringCategoryEntity> ScoringCategories { get; set; } | ||
|
@@ -50,4 +48,3 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2024 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information. | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
namespace Cite.Api.Data; | ||
public class CiteContextFactory : IDbContextFactory<CiteContext> | ||
{ | ||
private readonly IDbContextFactory<CiteContext> _pooledFactory; | ||
private readonly IServiceProvider _serviceProvider; | ||
public CiteContextFactory( | ||
IDbContextFactory<CiteContext> pooledFactory, | ||
IServiceProvider serviceProvider) | ||
{ | ||
_pooledFactory = pooledFactory; | ||
_serviceProvider = serviceProvider; | ||
} | ||
public CiteContext CreateDbContext() | ||
{ | ||
var context = _pooledFactory.CreateDbContext(); | ||
// Inject the current scope's ServiceProvider | ||
context.ServiceProvider = _serviceProvider; | ||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
namespace Cite.Api.Data; | ||
public class Entry | ||
{ | ||
public object Entity { get; set; } | ||
public EntityState State { get; set; } | ||
public IEnumerable<PropertyEntry> Properties { get; set; } | ||
private Dictionary<string, bool> IsPropertyModified { get; set; } = new(); | ||
public Entry(EntityEntry entry, Entry oldEntry = null) | ||
{ | ||
Entity = entry.Entity; | ||
State = entry.State; | ||
Properties = entry.Properties; | ||
ProcessOldEntry(oldEntry); | ||
foreach (var prop in Properties) | ||
{ | ||
IsPropertyModified[prop.Metadata.Name] = prop.IsModified; | ||
} | ||
} | ||
private void ProcessOldEntry(Entry oldEntry) | ||
{ | ||
if (oldEntry == null) return; | ||
if (oldEntry.State != EntityState.Unchanged && oldEntry.State != EntityState.Detached) | ||
{ | ||
State = oldEntry.State; | ||
} | ||
var modifiedProperties = oldEntry.GetModifiedProperties(); | ||
foreach (var property in Properties) | ||
{ | ||
if (modifiedProperties.Contains(property.Metadata.Name)) | ||
{ | ||
property.IsModified = true; | ||
} | ||
} | ||
} | ||
public string[] GetModifiedProperties() | ||
{ | ||
return IsPropertyModified | ||
.Where(x => x.Value) | ||
.Select(x => x.Key) | ||
.ToArray(); | ||
} | ||
} |
158 changes: 0 additions & 158 deletions
158
Cite.Api/Infrastructure/EventHandlers/EntityTransactionInterceptor.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.