Skip to content

Instantly share code, notes, and snippets.

@RobertBonham
Last active December 25, 2015 14:59
Show Gist options
  • Save RobertBonham/6995340 to your computer and use it in GitHub Desktop.
Save RobertBonham/6995340 to your computer and use it in GitHub Desktop.
DeleteData<T>(DbContext context) : Helper to Delete all data from a Entity Set. Good for small amounts of data. ContextDumpTest(DbContext context) : Helper to Dump the current status of the context SetSeedToZero GetTableName
#region *** Usings Directives ***
using System;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
#endregion
public static class ContextHelper
{
/// <summary>
/// Deletes all the data for a given Entity
/// </summary>
/// <typeparam name="T">An Entity Class</typeparam>
/// <param name="context">An DbContext</param>
/// <returns></returns>
public static void DeleteData<T>(DbContext context) where T : class
{
var oldData = context.Set<T>().ToList();
oldData.ForEach(row => context.Set<T>().Remove(row));
context.SaveChanges();
}
#region *** ContextDump ***
public static void ContextDump(DbContext context)
{
Debug.WriteLine("====Begin Context Dump========");
var dbsetList = context.ChangeTracker.Entries();
foreach (var dbEntityEntry in dbsetList)
{
Debug.WriteLine(dbEntityEntry.Entity.GetType().Name + " => " + dbEntityEntry.State);
switch (dbEntityEntry.State)
{
case EntityState.Detached:
case EntityState.Unchanged:
case EntityState.Added:
case EntityState.Modified:
WriteCurrentValues(dbEntityEntry);
break;
case EntityState.Deleted:
WriteSomeValues(dbEntityEntry);
break;
default:
throw new ArgumentOutOfRangeException();
}
Debug.WriteLine("==========End of Entity======");
}
Debug.WriteLine("==========End of Context======");
}
private static void WriteCurrentValues(DbEntityEntry dbEntityEntry)
{
foreach (var cv in dbEntityEntry.CurrentValues.PropertyNames)
{
Debug.WriteLine(cv + "=" + dbEntityEntry.CurrentValues[cv]);
}
}
private static void WriteSomeValues(DbEntityEntry dbEntityEntry)
{
foreach (var cv in dbEntityEntry.OriginalValues.PropertyNames)
{
Debug.WriteLine(cv + "=" + dbEntityEntry.OriginalValues[cv]);
}
}
#endregion
/// <summary>
/// Sets the Seed ID for a entity to 0 in the Database
/// </summary>
/// <typeparam name="T">An Entity Class</typeparam>
/// <param name="context">An DbContext</param>
public static void SetSeedToZero<T>(DbContext context) where T : class
{
var tablename = GetTableName<T>(context);
context.Database.ExecuteSqlCommand(string.Format("DBCC CHECKIDENT('{0}', RESEED, 0)", tablename));
}
/// <summary>
/// Gets the table name for a entity from a 'DbContext'
/// </summary>
/// <typeparam name="T">An Entity Class</typeparam>
/// <param name="context">An DbContext</param>
/// <returns>The Qualified Table Name from the DB eg. [dbo].[person]</returns>
public static string GetTableName<T>(this DbContext context) where T : class
{
ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext;
return objectContext.GetTableName<T>();
}
/// <summary>
/// Gets the table name for a entity from a 'ObjectContext'
/// </summary>
/// <typeparam name="T">An Entity Class</typeparam>
/// <param name="context">An ObjectContext</param>
/// <returns>The Qualified Table Name from the DB eg. [dbo].[person]</returns>
private static string GetTableName<T>(this ObjectContext context) where T : class
{
string sql = context.CreateObjectSet<T>().ToTraceString();
Regex regex = new Regex("FROM (?<table>.*) AS");
Match match = regex.Match(sql);
string table = match.Groups["table"].Value;
return table;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment