Posts

Showing posts with the label Entity Framework

DbEntityValidationException

When calling SaveChanges  on an entity context object you can sometimes get a DbEntityValidationException  error but it doesn't give you a convenient message as to what the error(s) were.  Here is a code snippet to get a convenient message from the DbEntityValidationException  object: Try context.SaveChanges() Catch ex As DbEntityValidationException 'Capture all default Entity Framework Validation errors like required fields, text size checks etc. Dim errorMessage As New StringBuilder For Each validationResult As DbEntityValidationResult In ex.EntityValidationErrors errorMessage.AppendLine( String .Format( "Entity of type [{0}] in state [{1}] has the following validation errors:" , validationResult.Entry.Entity. GetType .Name(), validationResult.Entry.State)) For Each item As DbValidationError In validationResult.Vali...

Avoid DTC call in Entity Framework 4 on SQL Server 2005

Ok really went down a rabbit hole today but I managed to crawl back out.  I went to create a database connection test. It consisted of first creating a transaction and then select a count of the number of records that match the record I was about to add, add the record, then count again to make sure the count is now different from the first count. This triggered a distributed transaction coordinator(DTC) error because I didn't have distributed transactions turned on but I didn't want it to start a distributed transaction as they add processing time and are not needed in this scenario.  So after much research I've discovered this apparently wouldn't have occurred if I was using SQL Server 2008 or greater but I of course was connecting to 2005. In Entity Framework 6(EF6) it looks like they are finally giving us the option to tell the Entity Framework to use an existing transaction and not force a distributed one. Which will be nice when EF6 is released and I upgrade ...