/// <summary> /// 打开连接 已赋值 connection 属性 /// </summary> /// <returns></returns> public virtual IDbConnection OpenConnection(bool isMaster = false, bool ignoreTransaction = false) { IDbConnection connection; if (ignoreTransaction || !InTransaction) { connection = new DataContext.DataContext(_configuration, isMaster, DbStoreKey, ConnectionFunc).DbConnection; } else { connection = Transaction.Connection; } if (connection == null) { throw new Exception("数据库连接创建失败,请检查连接字符串是否正确..."); } if (connection.State != ConnectionState.Open) { connection.Open(); } return(connection); }
public PersonServices(string collection) { var db = new DataContext.DataContext(); DB = db.Init(); LiteCollection = DB.GetCollection <T>(collection); }
public void Dispose() { if (context != null) { context.Dispose(); context = null; } }
public void SomeTest() { Database.SetInitializer(new TestDataContextInitializer()); DataContext.DataContext dc = new DataContext.DataContext(); Manufacturer manufacturer = dc.Manufacturers.Find(1); Assert.AreNotEqual(0, manufacturer.Models.Count); Assert.AreNotEqual(0, manufacturer.Models[0].AvailableEngines.Count); Assert.IsTrue(true); }
public ActionResult Index() { Database.SetInitializer(new TestDataContextInitializer()); DataContext.DataContext dc = new DataContext.DataContext(); IList<Manufacturer> manufacturers = dc.Manufacturers.ToList(); ViewBag.Message = "EFCodeFirst - Working With MVC4"; IList<SpatialModel> spatials = new List<SpatialModel>(); spatials.Add(new SpatialModel { SpatialText = string.Format("Ferrari is {0} miles from Lamborghini.", Math.Round(manufacturers[0].Location.Distance(manufacturers[1].Location) / 1609.344, 2)) }); spatials.Add(new SpatialModel { SpatialText = string.Format("Lamborghini is {0} miles from Aston Martin.", Math.Round(manufacturers[1].Location.Distance(manufacturers[2].Location) / 1609.344, 2)) }); return View(new EFCodeFirstViewModel{Manufacturers = manufacturers, Spatials = spatials}); }
public ActionResult ReadCars([DataSourceRequest] DataSourceRequest request) { List <Car> cars = null; using (var context = new DataContext.DataContext()) { cars = context.Cars.ToList(); } return(Json(cars.ToDataSourceResult(request), JsonRequestBehavior.AllowGet)); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Gets entity by identifier. </summary> /// /// <remarks> (MM), TechArk Solutions, 03/19/2014. </remarks> /// /// <param name="entityID"> Identifier for the entity. </param> /// /// <returns> The entity by identifier. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static async Task <TEntity> GetEntityByID(Int32 entityID) { TEntity entity = null; using (IDataContextAsync dataContext = new DataContext.DataContext()) { Repository <TEntity> repository = new Repository <TEntity>(dataContext); entity = await repository.FindAsync(entityID); } return(entity); }
public T Obter(int id) { try { using (var db = new DataContext.DataContext()) { var result = db.Set <T>().Find(id); return(result); } } catch (Exception ex) { throw new Exception(ex.Message); } }
static void Main(string[] args) { using (var file = new FileStream(ConfigurationManager.AppSettings["sourceFile"], FileMode.Open)) using (var db = new DataContext.DataContext()) { var data = CsvReader.ReadFromStream(file); foreach (var line in data) { Console.WriteLine(@"Adding: {0}, {1}", line[0], line[1]); db.Items.Add(new DataContext.Models.Entry { Title = line[0], Value = int.Parse(line[1]) }); } db.SaveChanges(); } }
public static async Task InsertMultipleEntities(IEnumerable <TEntity> entities) { using (IDataContextAsync dataContext = new DataContext.DataContext()) { Repository <TEntity> repository = new Repository <TEntity>(dataContext); using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(dataContext)) { //unitOfWork.BeginTransaction(); try { repository.InsertRange(entities); await unitOfWork.SaveChangesAsync(); } catch (Exception ex) { } } } }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Gets entity list for query. </summary> /// /// <remarks> (MM), TechArk Solutions, 03/19/2014. </remarks> /// /// <param name="query"> The query. </param> /// <param name="orderBy"> (Optional) amount to order by. </param> /// <param name="includes"> (Optional) the includes. </param> /// <param name="page"> (Optional) the page. </param> /// <param name="pageSize"> (Optional) size of the page. </param> /// /// <returns> The entity list for query. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public static (List <TEntity>, int) GetEntityListForQuery (Expression <Func <TEntity, Boolean> > query, Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > orderBy = null, List <Expression <Func <TEntity, Object> > > includes = null, Int32?page = null, Int32?pageSize = null) { List <TEntity> entities = null; int TotalRecords = 0; using (IDataContextAsync dataContext = new DataContext.DataContext()) { Repository <TEntity> repository = new Repository <TEntity>(dataContext); (IEnumerable <TEntity>, int)result = repository.SelectAsync(query, orderBy, includes, page, pageSize).Result; entities = result.Item1.ToList(); TotalRecords = result.Item2; } return(entities, TotalRecords); }
public static async Task DeleteRange(IEnumerable <TEntity> entities) { using (IDataContextAsync dataContext = new DataContext.DataContext()) { Repository <TEntity> repository = new Repository <TEntity>(dataContext); using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(dataContext)) { //unitOfWork.BeginTransaction(); try { foreach (var entity in entities) { repository.Delete(entity); } await unitOfWork.SaveChangesAsync(); } catch (Exception ex) { } } } }
public void UpdateAmountsSummary(ConceptModel concept) { using (DataContext.DataContext db = new DataContext.DataContext()) { SummaryModel summary = db.Summary.Where(t => t.SummaryId == concept.SummaryId).FirstOrDefault(); if (summary != null) { decimal?ConceptsPayd = 0; decimal?Prepayment = 0; summary.TotalIncome = db.Concept.Where(t => !t.EsEgreso).Sum(t => (decimal?)t.Amount); summary.TotalOutcome = db.Concept.Where(t => t.EsEgreso).Sum(t => (decimal?)t.Amount); //summary.TotalAmount = db.Concept.Where(t => t.SummaryId == summary.SummaryId && t.EsEgreso).Sum(t => (decimal?)t.Amount); ConceptsPayd = db.Concept.Where(t => t.SummaryId == summary.SummaryId && t.Payd && t.EsEgreso).Sum(t => (decimal?)t.Amount) ?? (decimal)0; Prepayment = db.Concept.Where(t => t.SummaryId == summary.SummaryId && !t.Payd && t.Prepayment > 0).Sum(t => (decimal?)t.Prepayment) ?? (decimal)0; summary.ConceptsPayd = ConceptsPayd.Value + Prepayment; db.SaveChanges(); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Perfom audited data operation. </summary> /// /// <remarks> (MM), TechArk Solutions, 03/19/2014. </remarks> /// /// <exception cref="Exception"> Thrown when an exception error condition occurs. </exception> /// /// <param name="entity"> The entity. </param> /// <param name="auditUserID"> Identifier for the audit user. </param> /// <param name="changeAction"> The change action. </param> /// /// <returns> A Task. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// private static async Task PerfomAuditedDataOperation(TEntity entity, ChangeAction changeAction) { try { using (IDataContextAsync dataContext = new DataContext.DataContext()) { Repository <TEntity> repository = new Repository <TEntity>(dataContext); using (IUnitOfWorkAsync unitOfWork = new UnitOfWork(dataContext)) { //unitOfWork.BeginTransaction(); try { if ((changeAction == ChangeAction.Deleted)) { repository.Delete(entity); await unitOfWork.SaveChangesAsync(); } if ((changeAction == ChangeAction.Updated)) { // Update the database entity. repository.Update(entity); // Save changes. await unitOfWork.SaveChangesAsync(); } else if (changeAction == ChangeAction.Created) { repository.Insert(entity); // Save the entity. This will trigger the setting of the ID property value. var changes = await unitOfWork.SaveChangesAsync(); } // Commit the transaction. //unitOfWork.Commit(); } catch (Exception ex) { // Was this a DbEntityValidationException exception? DbEntityValidationException entityValidationException = (ex as DbEntityValidationException); if (entityValidationException != null) { foreach (DbEntityValidationResult validationErrors in entityValidationException.EntityValidationErrors) { foreach (DbValidationError validationError in validationErrors.ValidationErrors) { System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } // Rollback transaction unitOfWork.Rollback(); // Rethrow transaction. throw ex; } } } } catch (Exception ex) { throw ex; } }
public ReviewsRepository(DataContext.DataContext context) : base(context) { }
public PositionService(DataContext.DataContext dataContext) { _dataContext = dataContext; }
public UnitService(DataContext.DataContext dataContext) { _dataContext = dataContext; }
public Test() { _dataContext = new DataContext.DataContext(); }
public DataRepository(DataContext.DataContext context) { _context = context; }
public PersonServices() { var db = new DataContext.DataContext(); DB = db.Init(); }
public TransactionRepositories(DataContext.DataContext _context) { db = _context; }
public CriteriasRepository(DataContext.DataContext context) : base(context) { }
public UserRepositories(DataContext.DataContext _context) { db = _context; }
public DepartmentManager(DataContext.DataContext dataContext, IMapper mapper) { _dataContext = dataContext; _mapper = mapper; }
public UnitOfWork(DataContext.DataContext _context) { db = _context; }
public Repository() { _context = new DataContext.DataContext(); _db = _context.Set <T>(); }
public AccountRepositories(DataContext.DataContext _context) { db = _context; }
public WaterObjectsRepository(DataContext.DataContext context, IAppMemoryCache cache) : base(context) { _cache = cache; InitRepository(); }