You receive an error message when you run an application, script, or functionality from an earlier SQL Server installation that uses common language runtime features in SQL Server 2008 or in SQL Server 2008 R2: "A .NET Framework error occurred"
After you upgrade to Microsoft SQL Server 2008 or to Microsoft SQL Server 2008 R2, when you run an application, a script, or a functionality from an earlier Microsoft SQL Server installation that uses common language runtime (CLR) features, you receive an error message that resembles the following error message:
Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate routine name:
System.InvalidOperationException: Data access is not allowed in this context. Either the context is a function or method not marked with DataAccessKind.Read or SystemDataAccessKind.Read, is a callback to obtain data from FillRow method of a Table Valued Function, or is a UDT validation method.
For example, this problem may occur in the following scenarios.
You use a method that has the following characteristics:
The method uses a CLR user-defined function, a user-defined type (UDT) method, or a user-defined aggregate that performs impersonation.
The method uses a large object (LOB) as a parameter.
The method does not use the DataAccesKind.Read property on the method.
You use the INIT method in a CLR table-valued function that performs impersonation.
You use the FillRow method in a CLR table-valued function that performs impersonation.
This problem occurs because of a change in the Database Engine features in SQL Server 2008 and in SQL Server 2008 R2.
To work around the problem as described in the scenarios in the "Symptoms" section, use one of the following methods.
Method 1: To work around this problem in Scenario 1 and in Scenario 2
To work around this problem, add the DataAccessKind.Read property to the method.
Method 2: To work around this problem in Scenario 3
To work around this problem, do the following:
Remove impersonation from the FillRow method.
Do not access external resources by using the FillRow method.
Access external resources only by using the INIT method of the table-valued function.
For information about the DataAccessKind.Read property and other custom attributes for CLR routines, visit the following Microsoft TechNet Web site: