Visual Studio publishing error: The specified file could not be encrypted

August 22, 2013 at 3:20 PMMadestro

I was trying to publish a web application the other day and came across an interesting error with some of the files:

The specified file could not be encrypted.

The solution turned out to be pretty simple:

1. Go to each file (or folder) giving you issues

2. Right click and select Properties

3. Hit the Advanced button

4. Uncheck "Encrypt contents to secure data"

5. Click OK, then OK on the other dialog (or APPLY)

6. Open (or preview) any file in the folder giving you issues

So why does this happen?

If Windows is storing your files in encrypted form, when the publish process attempts to copy the file, it tries to encrypt the file again and you get this error.

After clearing the encryption from the files, I was still experiencing the issue. It then occurred to me that the files might not get "decrypted" until I open them again which seems to be the case.

Maybe there is another reason but the bottom line is that opening them after unchecking the box in the properties made it work for me.

Good luck!

Posted in:


EF error: OriginalValues cannot be used for entities in the Added state

August 19, 2013 at 5:23 PMMadestro

I was working on a repository and came across an interesting, cryptic error that resulted from a fairly simple operation: Saving the changes.

Without getting into code details (which are  irrelevant to the point), the call to the following statement failed:


The error message read: OriginalValues cannot be used for entities in the Added state

The issue turned out to be that I had a field of type DateTime on SQL server that was set to NOT NULL. I forgot to assign the value to the entity field before calling SaveChanges() which resulted in a default DateTime value on the entity that was being passed on to SQL. This value was outside the range of valid SQL date values. 

Once I set the value on the field, the call to SaveChanges() completed without a problem.

I have been doing some more research on the subject and my understanding so far is that this situation can also be encountered if you have fields with default values on SQL Server. Because EF does not know about these default values, even if they exist, EF will force the value from the entity (like in my case), including situations where you have a value of NULL.

If anyone else can shed more light on the subject, please feel free to comment.


Posted in: Entity Framework