.NET Core中的ADO.NET是否可能? [英] Is ADO.NET in .NET Core possible?

查看:82
本文介绍了.NET Core中的ADO.NET是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用最新版本的ASP.NET Core,因为我想用它创建一组Web API.但是,我发现的教程主要针对实体框架.我不能使用它,因为我已经有一个旧版"数据库,因此代码优先"方法不是一种选择.

I would like to use the most recent versions of ASP.NET Core, because I want to create a set of Web APIs with it. However, the tutorials that I found are strongly focused on Entity Framework. I can't use that because I already have a "legacy" database, so a Code-First approach is not an option.

我的想法是使用ADO.NET连接到我的数据库,但是我不知道System.Data.SqlClient在ASP.NET Core项目中是否可用.我已经发现当我使用.NET Framework项目模板时该功能可用,但是在.NET Core项目中该功能仍然可用吗?

My idea is to use ADO.NET connections to my database, however I don't know if System.Data.SqlClient is available in a ASP.NET Core project. I already found that is available when I use a .NET Framework project template but is it still available in a .NET Core project?

推荐答案

System.Data.SqlClient名称空间中仍然存在现有的SqlConnection和其他相关连接,并且应使用完整的框架或.NET Core正常工作.

The existing SqlConnection and other related connections still exists within the System.Data.SqlClient namespace and should work as expected using the full framework or .NET Core.

您只需要添加适当的引用并使用语句将其包括在内,例如通过System.Data.SqlClient命名空间(如下所示)在您的project.json文件中即可:

You'll just need to add the appropriate references and using statements to include it such as through the System.Data.SqlClient namespace as seen below in your project.json file :

,然后通过您惯用的语法进行调用:

and then call it via the syntax you are accustomed to :

using(var connection = new SqlConnection("{your-connection-string}"))
{
      // Do work here
}

只要您有一个有效的连接字符串来连接到现有的旧数据库,就可以了.

So as long as you have a valid connection string to connect to your existing legacy database, you should be just fine.

关于ORM使用情况

我还发现有些人正在使用Dapper(一种微型ORM) 替代实体框架,显然更加灵活.在那里 用它代替ADO.NET有什么好处?

I also found that some people are using Dapper, a Micro-ORM replacement for Entity Framework, apparenty more flexible. It is there any advantages of using it instead ADO.NET?

这些ORM(对象关系映射器)是方便且通常很强大的工具,可以更轻松地将现有数据库数据映射到特定的类和对象,从而使它们更易于使用(而不是通过数据读取器进行迭代,解析).每行并手动构建每个对象.

These ORMs (object-relational mappers) are handy and often powerful tools that can more easily map your existing database data to specific classes and objects, which can make them easier to use (as opposed to iterating through a data reader, parsing each of your rows and building each object manually).

就性能而言,它最终取决于查询要执行的操作. ADO.NET通常是最快的,因为它是到数据库的准系统连接,但是在某些情况下Dapper可以击败它.实体框架虽然非常有用,但通常在性能方面落后于其他人,仅仅是因为它是如此大的ORM.

As far as performance goes, it ultimately depends on what you are going to be doing with your queries. ADO.NET will generally be the fastest as it a bare-bones connection to the database, however in some scenarios Dapper can actually beat it out. Entity Framework, while very useful, generally trails behind in performance, simply because it is such a large ORM.

再次-这最终取决于您在做什么,但是所有选项都是可行的.

Again - it ultimately depends on what you are doing, but all are viable options.

这篇关于.NET Core中的ADO.NET是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆