0

我有一些代码,使连接到数据库,并执行CRUD操作什么的DbContext做在ADO.NET

请参阅下面的代码:

我们在使用“的DbContext”的代码。 它是通用的,可以与各种数据库一起使用,也可以用于SQLServer,它的目的/任务是什么?

我以为的DbContext只与使用实体框架

public class UserRepository : Repository<User> 
{ 
    private DbContext _context; 
    public UserRepository(DbContext context) 
     : base(context) 
    { 
     _context = context; 
    } 


    public IList<User> GetUsers() 
    { 
     using (var command = _context.CreateCommand()) 
     { 
      command.CommandText = "exec [dbo].[uspGetUsers]"; 

      return this.ToList(command).ToList(); 
     } 
    } 

    public User CreateUser(User user) 
    { 
     using (var command = _context.CreateCommand()) 
     { 
      command.CommandType = CommandType.StoredProcedure; 
      command.CommandText = "uspSignUp"; 

      command.Parameters.Add(command.CreateParameter("@pFirstName", user.FirstName)); 
      command.Parameters.Add(command.CreateParameter("@pLastName", user.LastName)); 
      command.Parameters.Add(command.CreateParameter("@pUserName", user.UserName)); 
      command.Parameters.Add(command.CreateParameter("@pPassword", user.Password)); 
      command.Parameters.Add(command.CreateParameter("@pEmail", user.Email)); 

      return this.ToList(command).FirstOrDefault(); 


     } 

    } 


    public User LoginUser(string id, string password) 
    { 
     using (var command = _context.CreateCommand()) 
     { 
      command.CommandType = CommandType.StoredProcedure; 
      command.CommandText = "uspSignIn"; 

      command.Parameters.Add(command.CreateParameter("@pId", id)); 
      command.Parameters.Add(command.CreateParameter("@pPassword", password)); 

      return this.ToList(command).FirstOrDefault(); 
     } 
    } 


    public User GetUserByUsernameOrEmail(string username, string email) 
    { 
     using (var command = _context.CreateCommand()) 
     { 
      command.CommandType = CommandType.StoredProcedure; 
      command.CommandText = "uspGetUserByUsernameOrEmail"; 

      command.Parameters.Add(command.CreateParameter("@pUsername", username)); 
      command.Parameters.Add(command.CreateParameter("@pEmail", email)); 

      return this.ToList(command).FirstOrDefault(); 
     } 
    } 


} 

这里的DbContext类:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace DataAccessLayer 
{ 
    public class DbContext 
    { 


     private readonly IDbConnection _connection; 
     private readonly IConnectionFactory _connectionFactory; 
     private readonly ReaderWriterLockSlim _rwLock = new ReaderWriterLockSlim(); 
     private readonly LinkedList<AdoNetUnitOfWork> _uows = new LinkedList<AdoNetUnitOfWork>(); 

     public DbContext(IConnectionFactory connectionFactory) 
     { 
      _connectionFactory = connectionFactory; 
      _connection = _connectionFactory.Create(); 
     } 

     public IUnitOfWork CreateUnitOfWork() 
     { 
      var transaction = _connection.BeginTransaction(); 
      var uow = new AdoNetUnitOfWork(transaction, RemoveTransaction, RemoveTransaction); 

      _rwLock.EnterWriteLock(); 
      _uows.AddLast(uow); 
      _rwLock.ExitWriteLock(); 

      return uow; 
     } 

     public IDbCommand CreateCommand() 
     { 
      var cmd = _connection.CreateCommand(); 

      _rwLock.EnterReadLock(); 
      if (_uows.Count > 0) 
       cmd.Transaction = _uows.First.Value.Transaction; 
      _rwLock.ExitReadLock(); 

      return cmd; 
     } 

     private void RemoveTransaction(AdoNetUnitOfWork obj) 
     { 
      _rwLock.EnterWriteLock(); 
      _uows.Remove(obj); 
      _rwLock.ExitWriteLock(); 
     } 

     public void Dispose() 
     { 
      _connection.Dispose(); 
     } 
    } 
} 

回答

0

可以使用的EntityFramework到execute raw SQL,但看起来像你的榜样DbContext是不一个来自EntityFramework。它可以是您的项目中的其他库或自定义实现。您应该能够通过检查using进口或导航至DbContext定义来判断。