public static List <AuditLog> GetAuditLogRecordsForModifiedOrDeleted(DbEntityEntry dbEntry, Person person, ObjectContext objectContext) { var result = new List <AuditLog>(); var tableName = EntityFrameworkHelpers.GetTableName(dbEntry.Entity.GetType(), objectContext); if (!IgnoredTables.Contains(tableName)) { switch (dbEntry.State) { case EntityState.Deleted: var newAuditLog = CreateAuditLogEntryForDeleted(dbEntry, tableName, person, DateTime.Now, AuditLogEventType.Deleted); result.Add(newAuditLog); break; case EntityState.Modified: var modifiedProperties = dbEntry.CurrentValues.PropertyNames.Where(p => dbEntry.Property(p).IsModified).Select(dbEntry.Property).ToList(); var auditLogs = modifiedProperties.Select( modifiedProperty => CreateAuditLogEntryForAddedOrModified(objectContext, dbEntry, tableName, person, DateTime.Now, AuditLogEventType.Modified, modifiedProperty)) .Where(x => x != null) .ToList(); result.AddRange(auditLogs); break; } } // Otherwise, don't do anything, we don't care about Unchanged or Detached entities return(result); }
public async Task GetResponse_Valid() { using (var context = new ApplicationDbContext(EntityFrameworkHelpers.SqlDb())) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); await context.Database.MigrateAsync(); var record = new SongPlaybackTracker() { Id = "foo", SpotifySongUri = "test-spotify-song-uri", State = PlaybackState.Playing, Duration = TimeSpan.FromSeconds(30), StartedUtc = DateTime.UtcNow, PlaybackSession = null, PlaybackSessionId = null }; await context.SongPlaybackTrackers.AddAsync(record); await context.SaveChangesAsync(); var service = new SongPlaybackRepository(new NullLogger <SongPlaybackRepository>(), context); var result = await service.GetById("foo"); Assert.NotNull(result); Assert.Equal(result, record); await context.Database.EnsureDeletedAsync(); } }
public async Task GetResponse_Null() { using (var context = new ApplicationDbContext(EntityFrameworkHelpers.InMemoryOptions())) { var service = new SongPlaybackRepository(new NullLogger <SongPlaybackRepository>(), context); var result = await service.GetById("foo"); Assert.Null(result); } }
public async Task GetResponse_Null() { using (var context = await EntityFrameworkHelpers.SqlContextAsync()) { var songRepo = new SongPlaybackRepository(new NullLogger <SongPlaybackRepository>(), context); var sessionRepo = new PlaybackSessionRepository(new NullLogger <PlaybackSessionRepository>(), context, songRepo); var result = await sessionRepo.GetById("foo"); Assert.Null(result); } }
/// <summary> /// This has its own path because we need to call this after the initial savechanges to grab the new primary key id from the database /// </summary> public static List <AuditLog> GetAuditLogRecordsForAdded(DbEntityEntry dbEntry, Person person, ObjectContext objectContext) { var result = new List <AuditLog>(); var tableName = EntityFrameworkHelpers.GetTableName(dbEntry.Entity.GetType(), objectContext); if (!IgnoredTables.Contains(tableName)) { var modifiedProperties = dbEntry.CurrentValues.PropertyNames.Select(dbEntry.Property).Where(currentProperty => !IsPropertyChangeOfNullToNull(currentProperty)).ToList(); var auditLogs = modifiedProperties.Select( modifiedProperty => CreateAuditLogEntryForAddedOrModified(objectContext, dbEntry, tableName, person, DateTime.Now, AuditLogEventType.Added, modifiedProperty)) .Where(x => x != null) .ToList(); result.AddRange(auditLogs); } return(result); }
/// <summary> /// Method called when a process token executes the step. /// </summary> public ExitType Execute(IStepExecutionContext context) { if (string.IsNullOrEmpty(_efConnectString)) { string sqlInstance = PropertyHelper.GetString(context, "SqlInstance"); string sqlDbName = PropertyHelper.GetString(context, "SqlDbName"); // The name of the Entity Framework Model that we created. string efModel = "SimioServerModel"; _efConnectString = EntityFrameworkHelpers.BuildEfConnectionString(sqlInstance, sqlDbName, efModel); string startDt = PropertyHelper.GetString(context, "StartDateTime"); _dtStart = DateTime.Parse(startDt); string sqlConnectString = EntityFrameworkHelpers.BuildDbConnectionString(sqlInstance, sqlDbName); using (SqlConnection sqlConn = new SqlConnection(sqlConnectString)) { sqlConn.Open(); using (SqlCommand cmd = new SqlCommand("DELETE * FROM EntityVisits WHERE 1=1")) { int nn = cmd.ExecuteNonQuery(); } } } var entity = context.AssociatedObject; using (SimioCalculationStepSample2Entities efContext = new SimioCalculationStepSample2Entities(_efConnectString)) { var server = efContext.Servers.SingleOrDefault(rr => rr.Name == ""); // First see if this Enity exists EntityVisit ev = new EntityVisit(); ev.EntityId = EntityFrameworkHelpers.BuildEntityKey(entity.HierarchicalDisplayName); ev.ArrivalTime = _dtStart.AddSeconds(context.Calendar.TimeNow); ev.DepartureTime = null; efContext.EntityVisits.Add(ev); efContext.SaveChanges(); } return(ExitType.FirstExit); }
public async Task Upsert_New() { using (var context = await EntityFrameworkHelpers.SqlContextAsync()) { var original = new PlaybackSession() { Id = "foo", SpotifyPlaylistId = "spotify-playlist", DiscordVoiceChannelId = "discord-channel-id" }; var songRepo = new SongPlaybackRepository(new NullLogger <SongPlaybackRepository>(), context); var sessionRepo = new PlaybackSessionRepository(new NullLogger <PlaybackSessionRepository>(), context, songRepo); await sessionRepo.UpsertSession(original); var result = await sessionRepo.GetById("foo"); Assert.NotNull(result); Assert.Equal(result, original); } }
public async Task Upsert_New_WithCurrentPlayback() { using (var context = await EntityFrameworkHelpers.SqlContextAsync()) { var originalSongTracker = new SongPlaybackTracker() { Id = "foo-song", SpotifySongUri = "test-spotify-song-uri", State = PlaybackState.Playing, Duration = TimeSpan.FromSeconds(30), StartedUtc = DateTime.UtcNow }; var original = new PlaybackSession() { Id = "foo", SpotifyPlaylistId = "spotify-playlist", DiscordVoiceChannelId = "discord-channel-id", CurrentSongPlayback = originalSongTracker }; var songRepo = new SongPlaybackRepository(new NullLogger <SongPlaybackRepository>(), context); var sessionRepo = new PlaybackSessionRepository(new NullLogger <PlaybackSessionRepository>(), context, songRepo); await sessionRepo.UpsertSession(original); var result = await sessionRepo.GetById("foo"); var songResult = await songRepo.GetById("foo-song"); Assert.NotNull(result); Assert.Equal(result, original); Assert.NotNull(result.CurrentSongPlayback); Assert.Equal(result.CurrentSongPlayback, originalSongTracker); Assert.Equal(result, songResult.PlaybackSession); } }