简体   繁体   中英

SQL Server database project & .NET Framework 3.5 can't use TimeZoneInfo class

Built and deployed successfully to a SQL Server 2012 database using .NET framework 4.5

Tried to deploy to SQL Server 2008 database and realised .NET Framework 3.5 is required. Went into Dev machine (Win 7) Windows features and fully activated 3.5

A test Windows application project can declare a TimeZoneInfo class object when its .NET framework property is 3.5

The SQL Server Database Project can't, all instances of TimeZoneInfo in the code are now red lined.

So it seems the .NET Framework 3.5 has a restricted 'access' from a SQL Server Database Project

I have double checked that the target platform is SQL Server 2008.

Tried setting the SQLCLR Permission level to Unsafe. No difference.

Created a new dummy empty SQL Server Database Project, it behaves the same. Happy at 4.5 sad at 3.5.

I don't have earlier IDEs on this machine so can't say whether it is a VS2015 peculiarity or not.

FURTHER INFO which may help others; After pursuing Alexei's lead about references thereby getting a compilable project, I found the following on SO leading to a successful deployment on SqlServer 2008 database.

1) Leave the project permission level set to Safe. (You will make the assembly unsafe using SSMS later)

2) Using SSMS with sufficient privileges execute

  • EXEC sp_changedbowner 'sa' ALTER DATABASE MyDatabaseName SET TRUSTWORTHY ON

  • Alter Database [MyDatabaseName] set Trustworthy ON

3) Deploy the project

4) Using SSMS Execute

  • ALTER ASSEMBLY [MyAssemblyName] WITH PERMISSION_SET = UNSAFE;

The UDFs are now available.

Supposedly the TimeZoneInfo class has unsafe code in it so you have to do this to make it work with SQL Server 2008:

Because you are calling a method that has a HostProtectionAttribute (a few of the methods in TimeZoneInfo do), your assembly (the one that calls this method) must be cataloged as Unsafe also.

So my assumption is you need to turn ON the Project > Properties > Build > Allow Unsafe Code checkbox.

Please see this and this forum for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM