I'm trying to create an assembly from .NET Core dll in SQL Server 2017:
CREATE ASSEMBLY my_assembly
FROM 'C:\Temp\MyDll.dll' WITH PERMISSION_SET = SAFE;
This is the dll csproj :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="161.46041.41" />
</ItemGroup>
</Project>
When I'm running the query I get the following error:
Assembly 'MyAssembly' references assembly 'system.runtime, version=4.2.2.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: 2(The system cannot find the file specified.)). Please load the referenced assembly into the current database and retry your request.
How can I fix it?
Don't do that, basically. SQL CLR only supports .NET Framework, and AFAIK there are no plans to change that. Honestly, I'd probably just advise "avoid it altogether", and just use your database to ... store and query stuff. But if you really want to (and you understand how much pain it is going to cause you): you'll have to change your dll to target a version of .NET Framework (not .NET Core, or .NET 5 etc) that is available on the server. At a push you can target .NET Standard 2.0 and use that inside later versions of .NET Framework, but honestly: I don't recommend it - most of the .NET Standard support in .NET Framework is duck-tape surrounding lies holding up shims standing on hacks.