• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# DbEntityEntry类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中DbEntityEntry的典型用法代码示例。如果您正苦于以下问题:C# DbEntityEntry类的具体用法?C# DbEntityEntry怎么用?C# DbEntityEntry使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



DbEntityEntry类属于命名空间,在下文中一共展示了DbEntityEntry类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: GetRecordsForChange

        public static string GetRecordsForChange(DbEntityEntry dbEntry)
        {
            var returnvalue = "";
            var changeTime = DateTime.UtcNow;

            switch (dbEntry.State)
            {
                case System.Data.Entity.EntityState.Added:
                    returnvalue = string.Format("Add:{0};Value:{1} ", dbEntry.CurrentValues, dbEntry.OriginalValues);
                    break;

                case System.Data.Entity.EntityState.Modified:
                    foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
                    {
                        // For updates, we only want to capture the columns that actually changed
                        if (!object.Equals(dbEntry.OriginalValues.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName)))
                        {
                            returnvalue += string.Format("Modified:{0};OriginalValue:{1};NewValue:{2}|", propertyName, dbEntry.CurrentValues.GetValue<object>(propertyName), dbEntry.OriginalValues.GetValue<object>(propertyName));
                        }
                    }
                    break;
            }

            return returnvalue;
        }
开发者ID:nntu,项目名称:QLLoiWeb,代码行数:25,代码来源:LogsHelper.cs


示例2: ValidateEntity

        /// <summary>
        /// <see cref="DbContext.ValidateEntity"/>
        /// </summary>
        protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry,
            IDictionary<object, object> items)
        {
            if(entityEntry != null && entityEntry.State == EntityState.Added)
            {
                // Validation for duplicate email
                UserBase user = entityEntry.Entity as UserBase;
                if(user != null)
                {
                    if(Users.Any(u => u.Email.ToUpper() == user.Email.ToUpper()))
                    {
                        return new DbEntityValidationResult(entityEntry, new List<DbValidationError>())
                        {
                            ValidationErrors =
                            {
                                new DbValidationError("User",
                                    string.Format(CultureInfo.CurrentCulture, "Duplicate Email. Email: {0}", user.Email))
                            }
                        };
                    }
                }
            }

            return base.ValidateEntity(entityEntry, items);
        }
开发者ID:BiBongNet,项目名称:ASP.NET-Identity,代码行数:28,代码来源:IdentityContext.cs


示例3: TrySetVersion

 private static void TrySetVersion(DbEntityEntry entry, Func<int, int> versionSet)
 {
     var v = entry.TryGetProperty("Version");
     if (v != null && v.CurrentValue is int) {
         v.CurrentValue = versionSet((int)v.CurrentValue);
     }
 }
开发者ID:aritchie,项目名称:data,代码行数:7,代码来源:EntityVersionModule.cs


示例4: Remove

        public void Remove(DbEntityEntry entry)
        {
            var id = GetId(entry.Entity);

            var existing = Data.FirstOrDefault(x => x.Key.Equals(id));
            Data.Remove(existing);
        }
开发者ID:softwaretirol,项目名称:FakeEF,代码行数:7,代码来源:DbSetDataManager.cs


示例5: ValidateEntity

        protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry,
                                                                   IDictionary<object, object> items)
        {
            if (entityEntry.State != EntityState.Added)
            {
                return base.ValidateEntity(entityEntry,
                                           items);
            }

            var user = entityEntry.Entity as User;
            // Check for uniqueness of user name
            if (user == null || !Users.Any(u => String.Equals(u.UserName,
                                                              user.UserName,
                                                              StringComparison.CurrentCultureIgnoreCase)))
            {
                return base.ValidateEntity(entityEntry,
                                           items);
            }

            var result = new DbEntityValidationResult(entityEntry,
                                                      new List<DbValidationError>());
            result.ValidationErrors.Add(new DbValidationError("User",
                                                              "User name must be unique."));
            return result;
        }
开发者ID:johnazariah,项目名称:pelican,代码行数:25,代码来源:IdentityModels.cs


示例6: DbEntityEntryWrapper

        public DbEntityEntryWrapper(DbEntityEntry entity)
        {
            if (entity == null)
                throw new ArgumentNullException(nameof(entity));

            EntityEntry = entity;
        }
开发者ID:tomlane,项目名称:OpenRailData,代码行数:7,代码来源:DbEntityEntryWrapper.cs


示例7: GetEntityKeyDeleted

        /// <summary>
        /// Gets the entity key deleted.
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="entitySetName">Name of the entity set.</param>
        /// <returns></returns>
        private string GetEntityKeyDeleted(DbEntityEntry entry, out string entitySetName)
        {
            var keyBuilder = new StringBuilder();
            var keys = entry.Entity.GetType().GetProperties().Where(w => w.GetCustomAttributes(typeof(KeyAttribute), true).Any()).Select(p => p.Name).ToArray();

            entitySetName = ResolveEntitySetName(entry.Entity.GetType());

            foreach (var key in keys)
            {
                var objectVal = entry.Property(key).CurrentValue;

                if (objectVal == null)
                {
                    continue;
                }

                var keyValue = objectVal.ToString();

                if (keyBuilder.Length > 0)
                {
                    keyBuilder.Append(",");
                }

                keyBuilder.Append(keyValue);
            }

            return keyBuilder.ToString();
        }
开发者ID:Wdovin,项目名称:vc-community,代码行数:34,代码来源:LogInterceptor.cs


示例8: GetAuditLogs

        private static IEnumerable<AuditLog> GetAuditLogs(DbEntityEntry entityEntry, string userName, EntityState entityState, ObjectContext objectContext,
            DateTime auditDateTime)
        {
            var returnValue = new List<AuditLog>();
            var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator, objectContext);

            var auditedPropertyNames = GetDeclaredPropertyNames(entityEntry.Entity.GetType(), objectContext.MetadataWorkspace);
            foreach (var propertyName in auditedPropertyNames)
            {
                var currentValue = Convert.ToString(entityEntry.CurrentValues.GetValue<object>(propertyName));
                var originalValue = Convert.ToString(entityEntry.GetDatabaseValues().GetValue<object>(propertyName));
                if (entityState == EntityState.Modified)
                    if (originalValue == currentValue) //Values are the same, don't log
                        continue;
                returnValue.Add(new AuditLog
                {
                    KeyNames = keyRepresentation.Key,
                    KeyValues = keyRepresentation.Value,
                    OriginalValue = entityState != EntityState.Added
                        ? originalValue
                        : null,
                    NewValue = entityState == EntityState.Modified || entityState == EntityState.Added
                        ? currentValue
                        : null,
                    ColumnName = propertyName,
                    EventDateTime = auditDateTime,
                    EventType = entityState.ToString(),
                    UserName = userName,
                    TableName = entityEntry.Entity.GetType().Name
                });
            }
            return returnValue;
        }
开发者ID:johannbrink,项目名称:EF6Auditing,代码行数:33,代码来源:AuditLogBuilder.cs


示例9: ValidateEntity

        protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
        {
            var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>());

            if (entityEntry.Entity is Application && entityEntry.State == EntityState.Added)
            {
                Application app = entityEntry.Entity as Application;

                if (!app.IsValid)
                {
                    foreach (ValidationError ve in app.GetValidationErrors())
                    {
                        result.ValidationErrors.Add(new DbValidationError(ve.PropertyName, ve.ErrorMessage));
                    }
                }
            }

            if (result.ValidationErrors.Count > 0)
            {
                return result;
            }
            else
            {
                return base.ValidateEntity(entityEntry, items);
            }
        }
开发者ID:adback03,项目名称:MessageRoutingSystem,代码行数:26,代码来源:MrsContext.cs


示例10: ValidateEntity

        public static void ValidateEntity(DbContext context, DbEntityEntry entity, Type type)
        {
            if (entity.State == System.Data.EntityState.Modified)
            {
                if (!_parentAttributes.ContainsKey(type))
                {
                    var properties = from attributedProperty in type.GetProperties()
                                     select new
                                     {
                                         attributedProperty,
                                         attributes = attributedProperty.GetCustomAttributes(true)
                                             .Where(attribute => attribute is ParentAttribute)
                                     };
                    properties = properties.Where(p => p.attributes.Any());
                    _parentAttributes.Add(type,
                                          properties.Any()
                                              ? properties.First().attributedProperty.Name
                                              : string.Empty);
                }

                if (!string.IsNullOrEmpty(_parentAttributes[type]))
                {
                    if (entity.Reference(_parentAttributes[type]).CurrentValue == null)
                    {
                        context.Set(type).Remove(entity.Entity);
                    }
                }
            }
        }
开发者ID:WimAtIHomer,项目名称:GenericRepository,代码行数:29,代码来源:ParentValidator.cs


示例11: DbEntityValidationResult

        /// <summary>
        /// Creates an instance of <see cref="DbEntityValidationResult" /> class.
        /// </summary>
        /// <param name="entry"> Entity entry the results applies to. Never null. </param>
        /// <param name="validationErrors">
        /// List of <see cref="DbValidationError" /> instances. Never null. Can be empty meaning the entity is valid.
        /// </param>
        public DbEntityValidationResult(DbEntityEntry entry, IEnumerable<DbValidationError> validationErrors)
        {
            Check.NotNull(entry, "entry");
            Check.NotNull(validationErrors, "validationErrors");

            _entry = entry.InternalEntry;
            _validationErrors = validationErrors.ToList();
        }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:15,代码来源:DbEntityValidationResult.cs


示例12: GetTypeKey

 public string GetTypeKey(DbEntityEntry entry)
 {
     var objectStateEntry = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity);
     string type = string.Empty;
     if (objectStateEntry.EntityKey.EntityKeyValues != null)
         type = objectStateEntry.EntityKey.EntityKeyValues[0].Value.GetType().ToString();
     return type;
 }
开发者ID:victacora,项目名称:sifca,代码行数:8,代码来源:SIFCA_DATACONTEXT.cs


示例13: DbEntityValidationResult

        /// <summary>
        ///     Creates an instance of <see cref = "DbEntityValidationResult" /> class.
        /// </summary>
        /// <param name = "entry">
        ///     Entity entry the results applies to. Never null.
        /// </param>
        /// <param name = "validationErrors">
        ///     List of <see cref = "DbValidationError" /> instances. Never null. Can be empty meaning the entity is valid.
        /// </param>
        public DbEntityValidationResult(DbEntityEntry entry, IEnumerable<DbValidationError> validationErrors)
        {
            Contract.Requires(entry != null);
            Contract.Requires(validationErrors != null);

            _entry = entry.InternalEntry;
            _validationErrors = validationErrors.ToList();
        }
开发者ID:WangWilliam,项目名称:EntityFramework5,代码行数:17,代码来源:DbEntityValidationResult.cs


示例14: UpdateTrackedEntity

 /// <summary>
 /// Looks at everything that has changed and applies any further action if required.
 /// </summary>
 /// <param name="entityEntry"></param>
 /// <returns></returns>
 private static void UpdateTrackedEntity(DbEntityEntry entityEntry)
 {
     var trackUpdateClass = entityEntry.Entity as IModifiedEntity;
     if (trackUpdateClass == null) return;
     trackUpdateClass.ModifiedDate = DateTime.UtcNow;
     if (entityEntry.State == EntityState.Added)
         trackUpdateClass.rowguid = Guid.NewGuid();
 }
开发者ID:huoxudong125,项目名称:SampleMvcWebAppComplex,代码行数:13,代码来源:AdventureWorksLt2012.cs


示例15: AdjustTimestamps

        protected virtual void AdjustTimestamps(DbEntityEntry<IPersistenceAudit> entity)
        {
            if (entity.State == EntityState.Added)
                entity.Entity.CreatedTimestamp = entity.Entity.ModifiedTimestamp = DateTime.Now;

            if (entity.State == EntityState.Modified)
                entity.Entity.ModifiedTimestamp = DateTime.Now;
        }
开发者ID:kehinze,项目名称:Hermes,代码行数:8,代码来源:FrameworkContext.cs


示例16: AdjustUsers

        protected virtual void AdjustUsers(DbEntityEntry<IPersistenceAudit> entity)
        {
            if (entity.State == EntityState.Added)
                entity.Entity.CreatedBy = entity.Entity.ModifiedBy = CurrentUser.GetCurrentUserName();

            if (entity.State == EntityState.Modified)
                entity.Entity.ModifiedBy = CurrentUser.GetCurrentUserName();
        }
开发者ID:kehinze,项目名称:Hermes,代码行数:8,代码来源:FrameworkContext.cs


示例17: HandleSoftDelete

 private static void HandleSoftDelete(DbEntityEntry entry)
 {
     if (entry.Entity is ISoftDelete)
     {
         entry.State = EntityState.Unchanged;
         entry.Cast<ISoftDelete>().Entity.IsDeleted = true;
     }
 }
开发者ID:hersilio,项目名称:aspnetboilerplate,代码行数:8,代码来源:AbpDbContext.cs


示例18: InitCreationAudited

 /// <summary>
 /// 初始化创建审计信息
 /// </summary>
 private void InitCreationAudited(DbEntityEntry entry)
 {
     if ((entry.Entity is ICreationAudited) == false)
         return;
     var result = entry.Cast<ICreationAudited>().Entity;
     result.CreationTime = DateTime.Now;
     result.CreatorUserId = GetApplicationSession().Name;
 }
开发者ID:DukeCheng,项目名称:BeiDream,代码行数:11,代码来源:DbContextBase.cs


示例19: OnInsert

        public void OnInsert(DbEntityEntry entityEntry)
        {
            if (entityEntry.Entity is EntityWithCreatedDate)
                entityEntry.Cast<EntityWithCreatedDate>().Entity.Created = DateTime.Now;

            if (entityEntry.Entity is Entity)
                entityEntry.Cast<Entity>().Entity.Modified = DateTime.Now;
        }
开发者ID:Grinderofl,项目名称:YANBE,代码行数:8,代码来源:PreInsertEvent.cs


示例20: UpdateTimeStamp

        private static void UpdateTimeStamp(DbEntityEntry<IAuditInfo> entry)
        {
            if (entry.State == EntityState.Added)
            {
                entry.Entity.CreationDate = DateTime.Now;
            }

            entry.Entity.LastModified = DateTime.Now;
        }
开发者ID:JWroe,项目名称:JamieWroeDotCom,代码行数:9,代码来源:DataContext.cs



注:本文中的DbEntityEntry类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# DbExpression类代码示例发布时间:2022-05-24
下一篇:
C# DbContextOptionsBuilder类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap