Monday, July 29, 2013

DbEntityValidationException - Entity framework validation error handling

A sample code to catch part of an error handling when dealing with Entity Framework.
try { ... } catch (DbEntityValidationException dbEx) { #region LINQ query to get EF DB entity validation errors /* var errMsgs = dbEx.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage).ToArray(); var exceptionFullErrMsg = string.Concat( dbEx.Message, " Validation errors (DbEntityValidationException): " + UtilityHelper.ErrMsgBuilder(errMsgs)); log.Error(exceptionFullErrMsg); */ #endregion #region Loop to find Entity Framework DB entity validation errors foreach (var validationErrors in dbEx.EntityValidationErrors) foreach (var validationError in validationErrors.ValidationErrors) log.Error(UtilityHelper.ErrMsgBuilder(new string[] { "DbEntityValidationException" , validationError.ErrorMessage } )); #endregion } catch (Exception ex) { #region Exception logging log.Error(UtilityHelper.ErrMsgBuilder( new string[] { ex.GetType().Name } ), ex); #endregion } finally { ... } ... #region Utility Helper class public class UtilityHelper { public static string ErrMsgBuilder(String[] msgArray) { StringBuilder sbErr = new StringBuilder(); foreach (string msg in msgArray) sbErr.AppendLine(msg); return sbErr.ToString(); } } #endregion

Share/Bookmark

Friday, July 26, 2013

Unable to update the EntitySet 'EntityName' because it has a DefiningQuery and no element exists in the element to support the current operation.

Tied to debug my unit [TestMethod] in a unit test project through Test Explorer in Visual Studio 2012, then it tried to save the entity changes in database through a DbContext and it throw the folowing error message: 'Unable to update the EntitySet 'EntityName' because it has a DefiningQuery and no element exists in the element to support the current operation.'

In my case the problem was incorrect Entity Key setting on my entity in EF Model plus forgetting to set primary key in DB table. After setting primary key for the table which entity pointing too, I needed to update the model and open the EF model and manualy set Entity Key for primary key related member in the entity and clearing Entity Key setting for unnecessary entity members. Then rebuild, it worked. The reason was EF created a compound key made of all non nullable entity members which I had to reset them all.

It was my case but there are other possibilities for this error which already discussed if you search on Internet (Google or bing it)
Share/Bookmark

Wednesday, July 17, 2013

SQL - Record count for all tables in a DB

I needed to count the number of records after a DB backup and restore then the following script helped to do that.
USE [Database_Name] GO CREATE TABLE #tblRecCount ( tblName varchar(255), tblRowCount int ) EXEC sp_MSForEachTable @command1='INSERT #tblRecCount (tblName, tblRowCount) SELECT ''?'', COUNT(*) FROM ?' SELECT tblName, tblRowCount FROM #tblRecCount ORDER BY tblName, tblRowCount DESC SELECT SUM(tblRowCount) AS totalDbRowCount FROM #tblRecCount DROP Table #tblRecCount
The original post is available here.
Share/Bookmark

Tuesday, July 16, 2013

How to add a digital ID to adobe reader (.pfx exported certificate file)

When you see a PDF document after opening with a message that it’s signed but not valid, you need to import the necessary .pfx certificate file, if you have the .pfx file simply add it as following: Adobe reader :: Edit –> Protection –> Security Settings -> Windows Digital IDs then now click on “Add ID” and follow the wizard to impost the certificate.
Share/Bookmark