How to connect with SQL Server database using .NET Core Class Library (.NET Standard)?

狂风中的少年 提交于 2019-12-07 06:10:46

问题


I have just started to create an ASP.NET Core Web API Project. I am not much aware of "ASP.NET Core .NET Standard Library".

I am creating this application using Visual Studio 2017 RC and in the application, I have taken a project of type Class Library (.NET Standard) at repository layer.

Following is the screenshot for the same:

Now from repository Layer I want to connect to the database. I have created a variable

IDbConnection con;

Now I am trying to add reference of System.Data but I am unable to add any reference because when I am opening the add reference window then I am getting the following message:

No Framework assemblies were found on the machine.

How can I connect to database using .NET Core Class Library(.NET Standard)?


回答1:


.NET Standard Class libraries don't work by directly referencing a DLL, because with .NET Core there is no guarantee the framework will be installed on the system and .NET Core applications can also run as self-contained applications which ship the framework libraries with the application and do not require a runtime to be installed before.

You have to use the NuGet package manager (or project.json or *.csproj in VS2017) to add dependencies. For SQLServer you need to add the System.Data.SqlClient package (link) if you want to directly communicate with the Database (i.e. w/o an ORM).



来源:https://stackoverflow.com/questions/41315727/how-to-connect-with-sql-server-database-using-net-core-class-library-net-stan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!