如何使用.NET Core类库(.NET标准)与SQL Server数据库连接? [英] How to connect with SQL Server database using .NET Core Class Library (.NET Standard)?
问题描述
我刚刚开始创建一个ASP.NET Core Web API项目.我不太了解"ASP.NET Core .NET标准库".
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".
我正在使用Visual Studio 2017 RC创建此应用程序,并且在该应用程序中,我在存储库层接受了一个类库(.NET Standard)类型的项目.
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;
现在,我正在尝试添加 System.Data 的引用,但是我无法添加任何引用,因为当我打开添加引用窗口时,我得到了以下消息:
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:
在计算机上未找到Framework程序集.
如何使用.NET Core类库(.NET标准)连接到数据库?
How can I connect to database using .NET Core Class Library(.NET Standard)?
推荐答案
.NET标准类库无法通过直接引用DLL来工作,因为使用.NET Core不能保证将框架安装在系统上,并且.NET Core应用程序也可以作为独立的应用程序运行,这些框架库随应用程序一起提供,并且不需要之前安装运行时.
.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.
您必须使用NuGet软件包管理器(或VS2017中的project.json或* .csproj)添加依赖项.对于SQLServer,如果您需要添加System.Data.SqlClient
包(链接),希望直接与数据库通信(即不使用ORM).
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).
这篇关于如何使用.NET Core类库(.NET标准)与SQL Server数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!