5

I have Referenced MySql.Data on one project and Other project referenced nuget package which also referenced MySqlConnector inside of it. projects has dependency .

when i compile application im getting this error

enter image description here

This is application hierarchy

enter image description here

is there any way to avoid this? or did i do anything wrong when referencing packages?

Thanks

UPDATE this is the same namespaces from difference libs

enter image description here

UPDATE 2

This is the sample repo which reproduced same issue

13
  • 1
    Either use the fully qualified type name, or a using qualifier at the head of the cs file to disambiguate. Commented Feb 8, 2018 at 10:30
  • @Matt thanks. problem is both have same namespaces please have a look i have updated post. Commented Feb 8, 2018 at 10:36
  • Well then how is the compiler meant to know which one to use? Commented Feb 8, 2018 at 10:50
  • @ChrisL good question well that's the my problem as well Commented Feb 8, 2018 at 10:52
  • 1
    If you google "c# type exists in both assemblies" - you will find a lot of questions about the same issue. If nothing from those questions help - please include what you tried in question. Commented Feb 8, 2018 at 10:54

2 Answers 2

16

In NET.Framework projects you can go to the reference properties and set an alias for assembly. Net core projects doesn't fully support yet aliases for assemblies. But there is a workaround to use aliases in .net core. Edit your csproj file like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
...

  <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'MySqlConnector'">
        <Aliases>MySqlConnectorAlias</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>
...
</Project>

then in your cs file before all usings:

extern alias MySqlConnectorAlias;

then you can reference to you type from MySqlConnector like this:

MySqlConnectorAlias::MySql.Data.MySqlClient.MySqlConnection
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Albertk, I am trying to use your suggestion in my code as: MySqlConnectorAlias::MySql.Data.MySqlClient.MySqlConnection mysql = new MySqlConnectorAlias::MySql.Data.MySqlClient.MySqlConnection();and then get the error Cannot resolve symbole MySql. MySql.Data.MySqlClient are all read in my case. Can you please help? Thanks
It is hard to say for sure. Are you using Resharper? If yes then try to follow answers given here: 1, 2, 3
1

It will work If you remove mysql.data reference from Your project/references.

Hope it will work for you. for me it was worked. My project is ASP.NET Core Framework. Created project in VS2017 and opening in VS2019 at that time it introduced.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.