Exclude Property on Update in Entity Framework

后端 未结 5 1140
野性不改
野性不改 2020-11-28 04:53

I\'ve been looking for a proper way to mark a property to NOT be changed when updating a model in MVC.

For example, let\'s take this small model:

cla         


        
5条回答
  •  猫巷女王i
    2020-11-28 05:34

    I made an easy way to edit properties of entities I will share with you. this code will edit Name and Family properties of entity:

        public void EditProfileInfo(ProfileInfo profileInfo)
        {
            using (var context = new TestContext())
            {
                context.EditEntity(profileInfo, TypeOfEditEntityProperty.Take, nameof(profileInfo.Name), nameof(profileInfo.Family));
            }
        }
    

    And this code will ignore to edit Name and Family properties of entity and it will edit another properties:

        public void EditProfileInfo(ProfileInfo profileInfo)
        {
            using (var context = new TestContext())
            {
                context.EditEntity(profileInfo, TypeOfEditEntityProperty.Ignore, nameof(profileInfo.Name), nameof(profileInfo.Family));
            }
        }
    

    Use this extension:

    public static void EditEntity(this DbContext context, TEntity entity, TypeOfEditEntityProperty typeOfEditEntityProperty, params string[] properties)
       where TEntity : class
    {
        var find = context.Set().Find(entity.GetType().GetProperty("Id").GetValue(entity, null));
        if (find == null)
            throw new Exception("id not found in database");
        if (typeOfEditEntityProperty == TypeOfEditEntityProperty.Ignore)
        {
            foreach (var item in entity.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty))
            {
                if (!item.CanRead || !item.CanWrite)
                    continue;
                if (properties.Contains(item.Name))
                    continue;
                item.SetValue(find, item.GetValue(entity, null), null);
            }
        }
        else if (typeOfEditEntityProperty == TypeOfEditEntityProperty.Take)
        {
            foreach (var item in entity.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty))
            {
                if (!item.CanRead || !item.CanWrite)
                    continue;
                if (!properties.Contains(item.Name))
                    continue;
                item.SetValue(find, item.GetValue(entity, null), null);
            }
        }
        else
        {
            foreach (var item in entity.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty))
            {
                if (!item.CanRead || !item.CanWrite)
                    continue;
                item.SetValue(find, item.GetValue(entity, null), null);
            }
        }
        context.SaveChanges();
    }
    
    public enum TypeOfEditEntityProperty
    {
        Ignore,
        Take
    }
    

提交回复
热议问题