title: Using Entity Framework 6 / EF Core inside SQL CLR
body: <p>Microsoft Azure Tech support has confirmed <strong>that all version of .Net 4.XX including 4.7.2 is supported</strong> in SQL Server Managed Instance CLR.</p>
  
  <p>We're transforming some portion of Business layer with 4.7.2 and latest EF 6.2 </p>
  
  <p><a href="https://stackoverflow.com/questions/48284609/use-entity-framework-in-clr-stored-procedure">Use Entity Framework in CLR Stored procedure</a></p>
  
  <p><a href="https://patrickdesjardins.com/blog/how-to-use-third-party-dll-reference-in-a-sql-clr-function" rel="nofollow noreferrer">https://patrickdesjardins.com/blog/how-to-use-third-party-dll-reference-in-a-sql-clr-function</a></p>
  
  <p>Therefore in order to leverage existing investment in well tested code we want to move certain business layer into DB.</p>
  
  <p>However we ran into problems, when publishing.</p>
  
  <blockquote>
    <p>Creating [System.Dynamic]...</p>
    
    <blockquote>
      <p>Warning: The Microsoft .NET Framework assembly 'system.dynamic,
      version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.'</p>
    </blockquote>
    
    <p>You are registering is not fully tested in the SQL Server hosted environment and is not supported. In the future, if you upgrade or service this assembly or the .NET Framework, your CLR integration routine may stop working. Please refer SQL Server Books Online for more details.</p>
    
    <p>(47,1): SQL72014: .Net SqlClient Data Provider:    </p>
    
    <p>Msg 6218, Level 16, State 2, Line 1<br>
    <strong>CREATE ASSEMBLY for assembly 'System.Dynamic' failed because assembly 'System.Dynamic' failed verification.</strong> Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database.<br>
    CLR Verifier error messages if any will follow this message [ :<br>
    System.Dynamic.ArgBuilder::MarshalToRef][mdToken=0x6000002][offset
    0x00000000] Code size is zero. [ :
    System.Dynamic.ArgBuilder::UnmarshalFromRef][mdToken=0x6000003][offset</p>
  </blockquote>
  
  <p>P.S. (IMHO, doesn't matter if EF core as well, neverthless we're trying this with EF6.2)</p>
  
  <p>EDIT:
  Have given UNSAFE permission to all assemblies I'm referring to:
  Here are all the dependencies:</p>
  
  <p><a href="https://i.stack.imgur.com/ZnEEu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZnEEu.png" alt="enter image description here"></a></p>
  
  <pre><code>&lt;ItemGroup&gt;
      &lt;Reference Include="Microsoft.CSharp"&gt;
        &lt;HintPath&gt;..\packages2019\dotnet 4.7.2\Microsoft.CSharp.dll&lt;/HintPath&gt;
                  &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="System.Dynamic"&gt;
        &lt;HintPath&gt;..\packages2019\dotnet 4.7.2\System.Dynamic.dll&lt;/HintPath&gt;
                  &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="System.Runtime.Serialization"&gt;
        &lt;HintPath&gt;..\packages2019\dotnet 4.7.2\System.Runtime.Serialization.dll&lt;/HintPath&gt;
                  &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="SMDiagnostics"&gt;
        &lt;HintPath&gt;..\packages2019\v4.0.30319\SMDiagnostics.dll&lt;/HintPath&gt;
                  &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="System.ServiceModel.Internals"&gt;
        &lt;HintPath&gt;..\packages2019\v4.0.30319\System.ServiceModel.Internals.dll&lt;/HintPath&gt;
                  &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
      &lt;/Reference&gt;
        &lt;Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"&gt;
        &lt;HintPath&gt;..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll&lt;/HintPath&gt;
              &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"&gt;
        &lt;HintPath&gt;..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll&lt;/HintPath&gt;
              &lt;SqlPermissionSet&gt;UNSAFE&lt;/SqlPermissionSet&gt;
        &lt;SpecificVersion&gt;True&lt;/SpecificVersion&gt;
        &lt;IsModelAware&gt;True&lt;/IsModelAware&gt;
        &lt;GenerateSqlClrDdl&gt;True&lt;/GenerateSqlClrDdl&gt;
      &lt;/Reference&gt;
  </code></pre>
  
  <p>EDIT 3: Unsafe code in Azure SQL Server MI
  <a href="https://i.stack.imgur.com/94p98.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/94p98.png" alt="enter image description here"></a></p>
  
  <p>EDIT 4:</p>
  
  <ul>
  <li>The show-stopper is: <code>System.RunTime.Serialization</code>, </li>
  </ul>
  
  <blockquote>
  <pre><code>CREATE ASSEMBLY [System.Runtime.Serialization]
      AUTHORIZATION [dbo]
      FROM 0x4D5...
      WITH PERMISSION_SET = UNSAFE;
  </code></pre>
  </blockquote>
  
  <p>which prevents me to create Entity Framework UNSAFE assemblies into DB. Can we get past system.Runtime.Serialization?</p>
  
  <pre><code>GO
  CREATE ASSEMBLY [EntityFramework]
      AUTHORIZATION [dbo]
      FROM 0x4D5A90...
      WITH PERMISSION_SET = UNSAFE;
  
  
  GO
  PRINT N'Creating [EntityFramework.SqlServer]...';
  
  
  GO
  CREATE ASSEMBLY [EntityFramework.SqlServer]
      AUTHORIZATION [dbo]
      FROM 0x4...
      WITH PERMISSION_SET = UNSAFE;
  </code></pre>
  
  <blockquote>
    <p>Warning: The Microsoft .NET Framework assembly
    'system.runtime.serialization, version=4.0.0.0, culture=neutral,
    publickeytoken=b77a5c561934e089.' you are registering is not fully
    tested in the SQL Server hosted environment and is not supported. In
    the future, if you upgrade or service this assembly or the .NET
    Framework, your CLR integration routine may stop working. Please refer
    SQL Server Books Online for more details. Msg 6218, Level 16, State 2,
    Line 11 CREATE ASSEMBLY for assembly <strong>'System.Runtime.Serialization'
    failed because assembly 'System.Runtime.Serialization' failed</strong>
    verification. Check if the referenced assemblies are up-to-date and
    trusted (for external_access or unsafe) to execute in the database.
    CLR Verifier error messages if any will follow this message [ :
    System.AppContextDefaultValues::PopulateDefaultValues][mdToken=0x6000001]
    [offset 0x00000000] Code size is zero.</p>
  </blockquote>