Friday, 8 June 2007

Sql Server problem when attaching db with CLR objects

If you detach and then attach again, even if on the same server, a SQLServer 2005 database that has CLR stored procedures and functions you can receive the following message:

Msg 10314, Level 16, State 11, Line 1
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65538. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error:
System.IO.FileLoadException: Could not load file or assembly 'etere.playout.sqlserver, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)

MS says that only assemblies with UNSAFE or EXTERNAL_ACCESS are affected by the problem. The following MS knowledge base article explains the situation:,

I've tried to change the db owner as the article says, but it won't be working for me... maybe beacuse I was using TRUSTWORTHY ON. Don't know. Anyway I've tried to reissue the TRUSTWORTHY ON properties into the DB and... magic ! now works.

so if you are using the TRUSTWORTHY ON remember to include it after reattaching the db:

EXEC sp_changedbowner '?????'