使用两个数据访问访问数据库(Ado.net和实体框架) [英] access database using two data access(Ado.net and Entity Framework)

查看:55
本文介绍了使用两个数据访问访问数据库(Ado.net和实体框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想知道我是否可以在同一个项目中使用两种不同的数据访问?



this是场景:



我在一个Web应用程序中工作,使用.net核心作为后端,Angular 2作为前端,现在在我的SQL中数据库我使用了很多存储过程,我使用[Code-First / DB优先]使用存储过程和视图时遇到了困难,而且我曾经在我之前的项目中使用本机
Ado.net sql命令。 / p>


所以当我需要使用MS Identity进行身份验证和授权时,我的问题出现了,我发现使用identiy与Entity Framework一起使用时,Identity将使用DBContext使用连接字符串来查询数据库。 / p>


我的观点是,如果我使用Identity框架使用的相同连接字符串来使用本机Ado.net sq命令查询我的数据库。



这是一个不好的做法,或者它会降低性能,如果我使用指向同一项目的SAME数据库的两个数据访问,是否会出现任何冲突。

解决方案

您可以使用EF后门使用EF Command对象使用EF正在使用的现有数据库连接来访问数据库。


https://blogs.msdn.microsoft.com/alexj/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-database/


但是,该示例使用了ObjectContext,这是在EF 6之前可以轻松完成的事情,但是仍然可以通过使用命名空间及其DLL以及显示的代码来获得ObjectContext的一致性我在EF 6中使用的只是看看我是否可以获得
连接,并且我能够获得连接。

 使用System。达ta.Entity; 
使用System.Data.Entity.Core.EntityClient;
使用System.Data.Entity.Infrastructure;
使用System.Data.SqlClient;

public List< DTOStudent> GetStudents()
{

var dtos = new List< DTOStudent>();

using(var context = new CUDataEntities())
{
var adapter =(IObjectContextAdapter)context;
var objectContext = adapter.ObjectContext;

var entityConn = objectContext.Connection as EntityConnection;
var dbConn = entityConn.StoreConnection as SqlConnection;

dbConn.Open();

var students = context.Students.ToList();

foreach(学生中的var)
{
var dto = new DTOStudent
{
StudentID = stud.StudentID,
FirstName = stud.FirstName,
LastName = stud.LastName,
EnrollmentDate = stud.EnrollmentDate
};

dtos.Add(dto);
}
}

返回dtos;
}


I'm wondering if i can work with two different data access in the same project?

this is the scenario:

I'm working in a web application using .net core as back-end and Angular 2 as front-end, now in my SQL database i use alot of Stored Procedures, i face a difficulty to use Stored Procedure and views using [Code-First/ DB first], and i used to work with native Ado.net sql commands in my previous project.

So my problem arised when i need to use the MS Identity for authentication and authorization, i figured out that using identiy worked with Entity Framework, Identity will use DBContext to query database using connection string.

My point is if i use the same connection string that is used by Identity framework to query my database with native Ado.net sq commands.

Is this a bad practice or it's slows the performance, is there any conflict will appeare if i use two data access pointing to the SAME database for the same project.

解决方案

You can use the EF backdoor to access the DB using SQL Command objects using the existing DB connection that EF is using.

https://blogs.msdn.microsoft.com/alexj/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-database/

However, the example uses the ObjectContext, which was something one could easily do prior to EF 6, but one can still get the ObjectContext conncetion by using the namespaces and their DLL(s) along with code shown that I used in EF 6 just see if I could get the connection, and I was able to get the connection.

using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;

public List<DTOStudent> GetStudents()
        {
           
            var dtos = new List<DTOStudent>();

            using (var context = new CUDataEntities())
            {
                var adapter = (IObjectContextAdapter)context;
                var objectContext = adapter.ObjectContext;

                var entityConn = objectContext.Connection as EntityConnection;
                var dbConn = entityConn.StoreConnection as SqlConnection;

                dbConn.Open();

                var students = context.Students.ToList();

                foreach(var stud in students)
                {
                    var dto = new DTOStudent
                    {
                        StudentID = stud.StudentID,
                        FirstName = stud.FirstName,
                        LastName = stud.LastName,
                        EnrollmentDate = stud.EnrollmentDate
                    };

                    dtos.Add(dto);
                }
            }

            return dtos;
        }


这篇关于使用两个数据访问访问数据库(Ado.net和实体框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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