コード例 #1
0
ファイル: EFRepository.cs プロジェクト: hoangvu37/ProjectLV
        /// <summary>
        /// Updates changes of the existing entity.
        /// The caller must later call SaveChanges() on the repository explicitly to save the entity to database
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="entity">The entity.</param>
        public void Update <TEntity>(TEntity entity) where TEntity : class
        {
            EFDbContext dbContext = GetEFDbContext <TEntity>();

            System.Data.Entity.Core.Objects.ObjectContext objContext = dbContext.ObjectContext;

            var fqen = GetEntityName <TEntity>();

            System.Data.Entity.Core.EntityKey key = objContext.CreateEntityKey(fqen, entity);

            object originalItem;

            if (objContext.TryGetObjectByKey(key, out originalItem))
            {
                System.Data.Entity.Core.EntityKeyMember[] keys = key.EntityKeyValues;

                foreach (System.Data.Entity.Core.EntityKeyMember keyMember in keys)
                {
                    PropertyInfo entityKeyProperty   = entity.GetType().GetProperty(keyMember.Key);
                    PropertyInfo originalKeyProperty = originalItem.GetType().GetProperty(keyMember.Key);
                    if (entityKeyProperty != null && originalKeyProperty != null)
                    {
                        object originalKeyValue = originalKeyProperty.GetValue(originalItem, null);
                        entityKeyProperty.SetValue(entity, originalKeyValue, null);
                    }
                }

                objContext.ApplyCurrentValues <TEntity>(key.EntitySetName, entity);
            }
        }
コード例 #2
0
ファイル: Entities.cs プロジェクト: Shikyoh/HSWB2B2C
        public virtual int Job_Account(DateTime?startDate, DateTime?endDate)
        {
            ObjectParameter objectParameter  = (startDate.HasValue ? new ObjectParameter("StartDate", startDate) : new ObjectParameter("StartDate", typeof(DateTime)));
            ObjectParameter objectParameter1 = (endDate.HasValue ? new ObjectParameter("EndDate", endDate) : new ObjectParameter("EndDate", typeof(DateTime)));

            System.Data.Entity.Core.Objects.ObjectContext objectContext = ((IObjectContextAdapter)this).ObjectContext;
            ObjectParameter[] objectParameterArray = new ObjectParameter[] { objectParameter, objectParameter1 };
            return(objectContext.ExecuteFunction("Job_Account", objectParameterArray));
        }
コード例 #3
0
ファイル: UnitOfWork.cs プロジェクト: wardawg/WepApi
        public UnitOfWork(IDbContext context)
        {
            this._context = context as ReposContext ?? new ReposContext();

            this._objectContext = ((IObjectContextAdapter)this._context).ObjectContext;

            if (this._objectContext.Connection.State != ConnectionState.Open)
            {
                this._objectContext.Connection.Open();
            }

            this._transaction = _objectContext.Connection.BeginTransaction();
        }
コード例 #4
0
        public TEntity GetByKey <TEntity>(object keyValue) where TEntity : class
        {
            EFDbContext dbContext = GetEFDbContext <TEntity>();

            System.Data.Entity.Core.Objects.ObjectContext objContext = dbContext.ObjectContext;

            System.Data.Entity.Core.EntityKey key = GetEntityKey <TEntity>(keyValue);

            if (objContext.TryGetObjectByKey(key, out object originalItem))
            {
                return((TEntity)originalItem);
            }
            return(default(TEntity));
        }
コード例 #5
0
        /// <summary>
        /// Set for every <see cref="System.Data.Entity.Core.Objects.ObjectSet{TEntity}" /> in context specific
        /// <see cref="System.Data.Entity.Core.Objects.MergeOption" /> option.
        /// </summary>
        /// <param name="target">Context to be modified.</param>
        /// <param name="mergeOption">Merge option setup.</param>
        public static void SetMergeOption(this System.Data.Entity.Core.Objects.ObjectContext target, MergeOption mergeOption)
        {
            var properties = TypeDescriptor
                             .GetProperties(target.GetType())
                             .OfType <PropertyDescriptor>()
                             .Where(x => typeof(ObjectQuery).IsAssignableFrom(x.PropertyType));

            foreach (var property in properties)
            {
                var set = property.GetValue(target) as ObjectQuery;
                if (set != null)
                {
                    set.MergeOption = mergeOption;
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// The event that triggers when
        /// the context savechanges method is called
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentObjectContext_SavingChanges(object sender, EventArgs e)
        {
            try
            {
                ChangeTracker.DetectChanges(); // Important!
                System.Data.Entity.Core.Objects.ObjectContext           ctx = ((IObjectContextAdapter)this).ObjectContext;
                List <System.Data.Entity.Core.Objects.ObjectStateEntry> objectStateEntryList =
                    ctx.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added
                                                                 | System.Data.Entity.EntityState.Modified
                                                                 | System.Data.Entity.EntityState.Deleted)
                    .ToList();

                foreach (System.Data.Entity.Core.Objects.ObjectStateEntry entry in objectStateEntryList)
                {
                    object[] list = entry.Entity.GetType().GetCustomAttributes(false);
                    // Only the models that marked with 'Auditable' attribute will be tracked
                    // Inside the model the properties that need to tracked needed to be marked with
                    // Auditable attribute
                    if (list.Count() == 0 || !((Nido.Common.Utilities.Attributes.AuditableAttribute)(list[0])).DoAudit)
                    {
                        continue;
                    }

                    TypeAttributes te    = entry.Entity.GetType().Attributes;
                    AuditTrail     audit = new AuditTrail();
                    audit.RevisionStamp = DateTime.Now;
                    audit.TableName     = entry.EntitySet.Name + entry.EntityKey;
                    audit.UserName      = UserName;
                    audit.SystemName    = SystemName;
                    if (!entry.IsRelationship)
                    {
                        switch (entry.State)
                        {
                        case System.Data.Entity.EntityState.Added:
                            // write log...
                        {
                            audit.NewData = GetEntryValueInString(entry);
                            audit.Actions = AuditActions.I.ToString();
                        }
                        break;

                        case System.Data.Entity.EntityState.Deleted:
                            // write log...
                        {
                            audit.TablePrimaryId = GetKeyValue(entry);
                            audit.OldData        = GetEntryValueInString(entry);
                            audit.Actions        = AuditActions.D.ToString();
                        }
                        break;

                        case System.Data.Entity.EntityState.Modified:
                        {
                            string xmlOld = "<?xml version='1.0' encoding='utf-16'?>  <" + (entry.EntitySet).Name
                                            + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";
                            string xmlNew = "<?xml version='1.0' encoding='utf-16'?>  <" + (entry.EntitySet).Name
                                            + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";

                            PropertyInfo[] propList = entry.Entity.GetType().GetProperties();
                            Dictionary <string, object> attrList = new Dictionary <string, object>();
                            foreach (PropertyInfo pInfo in propList)
                            {
                                object[] atts = pInfo.GetCustomAttributes(typeof(AuditableAttribute), false);
                                if (atts.Length > 0)
                                {
                                    attrList.Add(pInfo.Name, atts[0]);
                                }
                            }
                            int      i      = 0;
                            object[] listte = entry.GetUpdatableOriginalValues()[0].GetType().GetCustomAttributes(false);
                            foreach (string propertyName in
                                     entry.GetModifiedProperties())
                            {
                                if (attrList.Keys.Contains(propertyName))
                                {
                                    DbDataRecord original = entry.OriginalValues;

                                    string oldValue = original.GetValue(
                                        original.GetOrdinal(propertyName))
                                                      .ToString();

                                    System.Data.Entity.Core.Objects.CurrentValueRecord current = entry.CurrentValues;
                                    string newValue = current.GetValue(
                                        current.GetOrdinal(propertyName))
                                                      .ToString();

                                    xmlOld += "<" + propertyName + " type='" + original.GetFieldType(i) + "'>" + oldValue + "</" + propertyName + ">";
                                    xmlNew += "<" + propertyName + " type='" + original.GetFieldType(i) + "'>" + newValue + "</" + propertyName + ">";
                                }
                                i++;
                            }

                            xmlOld += "</" + (entry.EntitySet).Name + ">";
                            xmlNew += "</" + (entry.EntitySet).Name + ">";

                            audit.OldData = xmlOld;
                            audit.NewData = xmlNew;

                            audit.TablePrimaryId = GetKeyValue(entry);
                            audit.Actions        = AuditActions.U.ToString();
                            break;
                        }
                        }
                    }
                    AuditTrails.Add(audit);
                }
            }
            catch
            {
                // Keep quite...
            }
        }
コード例 #7
0
 public BaseObjectConext(System.Data.Entity.Core.Objects.ObjectContext objectContext, bool dbContextOwnConnection)
     : base(objectContext, dbContextOwnConnection)
 {
 }