EF - 在实体之间更改组件时的验证检查 [英] EF - Validation check when an assoication changes between entities

查看:39
本文介绍了EF - 在实体之间更改组件时的验证检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在继续保存数据之前,我想在用户在UI中更改外键值时运行自定义验证检查。如果数据不好,我想提出错误并阻止更新/插入/删除。例如,在Northwind数据库中,有如下产品和类别。

产品类别
===================================
1.胡萝卜蔬菜
2.苹果水果
3.橙子水果
4.花生坚果

每种产品都有与之相关的类别。当用户正在编辑"Apple"时UI中的产品数据并从"Fruit"中更改类别。到"蔬菜",我想运行验证检查并拒绝更改。此外,我想在接受或拒绝更改之前比较类别的原始值和当前值。我注意到更新后发生了AssoiciationChanged事件,因此并没有真正阻止更新。有一个简单的方法来做到这一点。在过去的4个小时里,我一直在拉头发,没有任何运气。请帮助。

I would like to run a custom validation check when a foreign key value is changed in the UI by the user, before proceeding to save the data. If the data is no good, I would like to raise the error and prevent the update/insert/delete. For example, in the Northwind database, there are Products and Categories as below. Products Category =================================== 1. Carrot Vegetable 2. Apple Fruit 3. Orange Fruit 4. Peanut Nut Each Product has a category associated with it. When the user is editing the "Apple" product data in the UI and changes the category from "Fruit" to "Vegetable", I would like to run a validation check and reject the change. Also I would like to compare the original value and the current value for the category before I accept or reject the change. I noticed the AssoiciationChanged event occurs after the update and therefore doesn't really prevent the updated. Is there an simple way to do this. I've been pulling my hair for the last 4 hours on this without any luck. Please help.

推荐答案

我一直在查看有关实体框架的各种文档,以了解如何执行此操作但仍然没有运气。以下是我经历的一些链接。

I've been looking through all kinds of documentation on Entity Framework to figure how to do this but still no luck. Here are few links that I have been going through.

如何:更改对象之间的关系(实体框架)

如何:保存更改时执行业务逻辑(实体框架)

如上所述,AssociationChanged事件不能正常工作。不确定我是否做错了什么。我注意到了..

AssociationChanged event doesn't quite work as mentioned in the above link. Not sure if I'm doing anything incorrectly. I noticed that.. 

1。在执行更新之前或之后,从不触发AssociationChanged事件(我在下拉框中更改了代表FK列的选定项目。)

1. The AssociationChanged event is never fired before or after the update is performed (I am changing the selected item in the dropdown box that would represent the FK column).

2。对于List页面中的每个记录都会触发AssociationChanged事件。每个详细信息,编辑,插入页面,但在单击更新链接按钮后永远不会触发。一旦将更新提交到数据库,它将仅在重定向到List页面时再次触发。似乎它完全绕过了我的更新操作。

2. The AssociationChanged event is fired for every record in the List page & once per Details, Edit, Insert pages but it is never fired after I click the update link button. It is fired again only on the redirect to the List page once the update is committed to the database. Seems like its completely bypassing my update action.

3。每当触发AssociationChanged事件时(对于上述操作),CollectionChangeAction永远不会设置为"Remove"。它始终设置为"添加"。

3. Whenever AssociationChanged event is fired (for the above mentioned actions), the CollectionChangeAction is never set to "Remove". It is always set to "Add".

4。虽然我可以在AssociationChanged事件触发时获取ForeignKey Id,但我无法弄清楚如何将此Id(int)转换为描述值,并且我没有Id的原始值和当前值。

4. Although I'm able to get the ForeignKey Id when the AssociationChanged Event does fire, I couldn't figure out how to convert this Id (int) to a description value, and I don't have the original and current values for the Id.

5。作为另一种方法,我能够拦截OnSavingChanges事件处理程序中的AssociationChange,并为关联(FK)列更改建立当前和原始值,但我仍然找不到将Ids(int)转换为适当的方法的方法描述值。

5. As another approach, I was able to intercept the AssociationChange in OnSavingChanges event handler and establish the current and original values for the association(FK) column change, but I still couldn't find a way to convert the Ids (int) to their appropriate description values.

如果有人能够成功验证EF的FK列更改,请告诉我。这是我的代码(无论我到目前为止,它仍然不完整)

Please let me know if anybody was able to validate the FK column changes for EF successfully. Here is my code (whatever I have so far, it is still incomplete) 

AssociationChanged事件

public partial class OFFICE
    {
        public OFFICE()
        {
            this.STATEReference.AssociationChanged +=new CollectionChangeEventHandler(STATEReference_AssociationChanged);
        }
        public void STATEReference_AssociationChanged(object sender,
           CollectionChangeEventArgs e)
        {
            if (e.Action == CollectionChangeAction.Remove)
            {
                this.OnPropertyChanging("STATE");
            }
            else if (e.Action == CollectionChangeAction.Add)
            {
                this.OnPropertyChanged("STATE");
            }
        }
        public class Office_MD
        {
        }

}

OnSavingChanges事件

 partial void OnContextCreated()
        {
            this.SavingChanges +=
                new System.EventHandler(OnSavingChanges);
        }

        public void OnSavingChanges(object sender,
                System.EventArgs e)
        {
            var stateManager =
                ((Entities2)sender).ObjectStateManager;
            var addedEntities =
                ObjectStateManager.GetObjectStateEntries(EntityState.Added);
            var deletedEntities =
                ObjectStateManager.GetObjectStateEntries(EntityState.Deleted);
            string originalValue;
            string currentValue;

            foreach (ObjectStateEntry stateEntryEntity in
                   addedEntities)
            {
                if (stateEntryEntity.IsRelationship && stateEntryEntity.EntitySet.Name == "OFFICE_STATECD_FK")
                {
                    EntityKey entityKeyFrom = (EntityKey)stateEntryEntity.CurrentValues[1];
                    EntityKey entityKeyTo = (EntityKey)stateEntryEntity.CurrentValues[0];
                    currentValue = entityKeyTo.EntityKeyValues[0].Value.ToString();
                }
            }

            foreach (ObjectStateEntry stateEntryEntity in
                   deletedEntities)
            {
                if (stateEntryEntity.IsRelationship && stateEntryEntity.EntitySet.Name == "OFFICE_STATECD_FK")
                {
                    EntityKey entityKeyFrom = (EntityKey)stateEntryEntity.OriginalValues[1];
                    EntityKey entityKeyTo = (EntityKey)stateEntryEntity.OriginalValues[0];
                    originalValue = entityKeyTo.EntityKeyValues[0].Value.ToString();
                }
            }
        }



这篇关于EF - 在实体之间更改组件时的验证检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆