Skip to content

Instantly share code, notes, and snippets.

@m4tt1mus
Created September 2, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4tt1mus/3b1f07112224c03c2297 to your computer and use it in GitHub Desktop.
Save m4tt1mus/3b1f07112224c03c2297 to your computer and use it in GitHub Desktop.
Catch EF Exceptions and report them in fixie
//catch EF Exceptions and report them in fixie
namespace Test.Tests
{
using System;
using System.Data.Entity.Validation;
using Fixie;
public class CustomConvention : Convention
{
public CustomConvention()
{
Classes.NameEndsWith("Tests");
Methods.Where(method => method.IsVoid() || method.IsAsync());
CaseExecution.Wrap<DbValidationDetailer>();
}
}
public class DbValidationDetailer : CaseBehavior
{
public void Execute(Case context, Action next)
{
next();
foreach (var ex in context.Exceptions)
{
var dbv = ex as DbEntityValidationException;
if (dbv == null) continue;
foreach (var err in dbv.EntityValidationErrors)
{
Console.WriteLine("Error on {0} entity: {1}", err.IsValid ? "valid" : "invalid", err.Entry);
foreach (var ve in err.ValidationErrors)
{
Console.WriteLine(" {0}: {1}", ve.PropertyName, ve.ErrorMessage);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment