Do you fix or a restore a Confused CRM Database?

Very odd situation occurred recently and left the poor CRM database in a state of confusion.

it started yesterday when I was getting an invalidCastingException when a plugin was running.

Unexpected exception from plug-in (Execute):  System.InvalidCastException: Unable to cast object of type ‘Microsoft.Xrm.Sdk.Entity’ to type ‘ActivityParty’

I couldn’t fathom why I was getting this error because I could walk through the code in a console app, calling the same business logic/class. I could see the entities file and it had the ActivityParty included in it, what the heck was the problem

The error above was happening yesterday, I came in today thinking about the error and cause must be linked to the entities file because this is used when creating and using early bound classes.  If I refresh the entities.class it should work.

 

crmsvcutil.exe
 

I had been running the crmsvcutil.exe but I hadn’t noticed it had been erroring, instead of refreshing the entities.class.

So today I started looking at the

I started when I was trying to build the early bound entities.class, I was getting an error

 

Exiting program with exception: Mapping failed for Microsoft.Crm.Metadata.EntityMetadata property Attributes with exception Microsoft.Crm.CrmException: Mapping failed for Microsoft.Crm.Metadata.AttributeMetadata property Description with exception System.ArgumentException: An item with the same key has already been added.

The only previous time I have seen duplicate, same key error is when you try and import a solution with a field which was deleted and recreated as a different type.

This error returns very little from the internet (never a good sign) but the one article I found which seemed to make sense was from CRM MVP David Jennaway
user had upgraded, the upgrade process had somehow created two metadata fields of the same type. It has an interesting script to find duplicates

Nothing is working

Developers can sometimes only access CRM from the SDK they forget to check things on the CRM front end e.g. the CRM Web application.

I went into CRM and found I couldn’t publish any customizations and in fact all the plugins were not working.

We quickly reached the opinion a server reboot might fix the problems and restarted it with the reason (Hosks big problem)

unfortunately this did not resolve the problem and what we think happened was CRM let us add two fields with the same name.  There were some modifications to one of the entities in the afternoon, which we think is probably the culprit

CRM had now decided to lock up good and proper, we could export any solutions to try and edit the

What to do next

It was a DEV solution were unmanaged.  This means deleting the solution won’t do anything to the customizations. If they were managed solutions deleting them would also remove the customizations (and the dodgy double field) but it would also remove all the data (hmm it might take a while to create/import all that data)

We couldn’t import a solution over the top, wait I will rephrase that, we could but importing solutions are an additive, which means they only add things, they will not remove them.

CRM 2013 – Understanding Solutions and how they work

Restore or Fix (Stick or Twist)

Sometimes you have to make a decision to rollback to a previous database backup.  When you add the time it would take to investigate and fix the current CRM solution/database compared to rolling back to a daily backup from yesterday or maybe the day before.

In this calculation you also need to think about how many people are working on the project because during investigation it means other developers had to stop.  We think we might have known the cause but fixing it looked like tampering with the database directly.  This isn’t a great solution because we might just be kicking the problems down the road because we couldn’t be sure (if we worked out how to fix the problem) when fixing the problem we were creating more problems which we would find later.

Sometimes it’s better to lose one day of customizations versus weeks/months of potentially corrupt customizations

This is also highlights the importance of checking in your code changes into source control.  Most of the changes I have made during the last couple of days are all held in source control, so I just need to redeploy.

Changes to CRM customizations are a bit more tricky e.g.

  • Entity changes
  • fields
  • views

These can only really be backed up by exporting the solutions they are held in, on a daily basis, although they are of course also backed up in the database itself.  It is possible to export solutions programatically, so you could create an application which you could setup to a windows daily task (although I haven’t yet seen anyone do this)

The morale of the story is, make sure you put your changes in source control because the server could get corrupted or your own computer could crash or get lost and then you could be in real trouble

picture confused from here

picture wall from here
stick or twist picture

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.