Runtime: The type 'SqlDbType' is defined in both System.Data.SqlClient and netstandard 2.0

Created on 16 Aug 2017  路  3Comments  路  Source: dotnet/runtime

Noticed this while updating a library which uses Dapper (which is compatible with .NET standard 1.3+)

Steps to reproduce:

  • Create library targeting .NET standard 2.0
  • Reference Dapper
  • Add private SqlDbType _Type = SqlDbType.BigInt; to class in library

Expected result:

  • No errors

Actual result:
CS0433 The type 'SqlDbType' exists in both 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

As far as I understand I should be able to reference any .NET standard library with a lower version number from 2.0. In this case Dapper. Dapper uses System.Data.SqlClient, but in 2.0 the SQL types moved into .NET Standard 2.0.
I guess the System.Data.SqlClient nuget package is missing .NET standard 2.0 type forwards.

area-System.Data.SqlClient

Most helpful comment

I repro this. It is consuming this one:
C:\Users\danmose.nuget\packages\system.data.sqlclient\4.1.0\ref\netstandard1.3System.Data.SqlClient.dll

Indeed that one has no type forwards. To solve the problem, add a reference to the System.Data.SqlClient package itself (4.4.0). This does have the type forwards.

@weshaggard I believe this is inevitable until Dapper updates their package to have a netstandard2.0 asset, which references 4.4.0 ?

All 3 comments

I repro this. It is consuming this one:
C:\Users\danmose.nuget\packages\system.data.sqlclient\4.1.0\ref\netstandard1.3System.Data.SqlClient.dll

Indeed that one has no type forwards. To solve the problem, add a reference to the System.Data.SqlClient package itself (4.4.0). This does have the type forwards.

@weshaggard I believe this is inevitable until Dapper updates their package to have a netstandard2.0 asset, which references 4.4.0 ?

@danmosemsft Thank you very much, your solution indeed solves my problem. I'll close this bug.

If you are targeting more than one framework, like:

<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>

then you need to remember to conditional add the System.Data.SqlClient package reference:

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
   <PackageReference Include="System.Data.SqlClient" Version="4.4.0" /> 
</ItemGroup>
Was this page helpful?
0 / 5 - 0 ratings