Created
September 2, 2015 15:03
-
-
Save m4tt1mus/3b1f07112224c03c2297 to your computer and use it in GitHub Desktop.
Catch EF Exceptions and report them in fixie
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
//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