How can we use managed nuget packages in c++/cli project

S D_123 1 Reputation point
2021-02-01T12:12:36.627+00:00

I want to consume managed nuget package in c++/cli project. Is there a way to do that?

For example

  1. I have created a C# project(MainProject) and added EntityFramework nuget package to that project.
  2. I have created one more C# project(TestCSProject) and added MainProject as reference to that project. Then automatically in references entityframework is also added
  3. I have created one C++/CLI project(TestCLIProject) and added MainProject as reference to that project so that I want to see whether I can use entityframework.

But that didnt happened.

So I want to know how can I use managed nuget package in c++/cli project

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 27,356 Reputation points Microsoft Vendor
    2021-02-02T08:37:09.69+00:00

    Hi @S D_123 ,

    C++/CLI project is different from C# project and you could not use EntityFramework directly by adding other project references which include the EntityFramework NuGet Package or dll files.

    You can try these steps:

    1). Create a C++/CLI project > Add a new C# project to the same solution > Add NuGet Package to the C# project.

    2). Go to the folder of C# project > copy the packages.config(I created a .net framework C# project) file to the C++/CLI project directory.

    3). Add this packages.config file to your C++/CLI project in VS(from Solution Explorer > click Show All Files button > right-click the packages.config > choose Include In Project).

    4). From your C++/CLI project > right-click Reference > Add Reference > Browse > Browse and choose the related dll files which are provided(located) by the C# project > Add > OK.

    5). Then you can use C++/CLI syntax like using namespace XXXXX to use it in your C++/CLI project.

    Although this can make it work, there may exist some issues. For example, updating NuGet packages will be a little troublesome.

    Best Regards,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments