实体字段上的验证将停止保存编辑屏幕。 [英] Validation on Entity Field stops save of edit screen.

查看:75
本文介绍了实体字段上的验证将停止保存编辑屏幕。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个名为adDetail的实体中,我有一个不应该有重复值的非身份字段。


adShortURL。


 


我有一个自定义的新数据和一个自定义编辑屏幕,附加到名为adDetail的实体。


 


使用新数据屏幕时。  该领域的验证效果很好。  甚至到数据库检查重复值。


 


但是,当我去编辑同一个表时现有数据,重复值验证会阻止屏幕保存。


 


试图确定屏幕名称,所以放一个if验证。  没找到方法。  尝试设置屏幕参数,以便我可以在验证中查找它。  
不起作用。  自定义验证方法中没有可用的屏幕参数。 (可能不知道在哪里看)。


 


请帮我弄清楚如何验证添加并跳过编辑验证屏幕。


 


< p style ="margin:0in; line-height:13pt; font-family:Verdana; font-size:9pt">谢谢,


Victor


 


Code Snippet:


  partial void AdShortURL_Validate(EntityValidationResultsBuilder results)


  &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // results.AddPropertyError("< Error-Message>");


  ;


  &NBSP; &NBSP; &NBSP; &NBSP; // ***这里有一个Bug只发生在编辑屏幕上。  在编辑屏幕上,检查发生并在数据库中发现禁止保存的副本。


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //点击数据库,看看那里是否已存在值,就像我输入的那样。


&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IDataServiceQueryable< LSAR_AdDetail> adDetails =


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.DataWorkspace.OtLY83U6SQ72SZCG9OtData.LSAR_AdDetails.Where(i => i.AdShortURL == this.AdShortURL);

解决方案

我认为这是因为AdShortURL_Validate中的查询是在数据源中找到相同的实体,并且无法识别它本身。这就是为什么你没有在添加新的AdDetail实体时遇到错误(因为它在
数据源中不存在),并且在编辑现有实体时会出现错误。


我会建议在保存后更改您的查询以考虑实体的自身,例如:

 IDataServiceQueryable< LSAR_AdDetail> adDetails = null; 
if(this.Details.EntityState == EntityState.Added)
adDetails = this.DataWorkspace.OtLY83U6SQ72SZCG9OtData.LSAR_AdDetails.Where(i => i.AdShortURL == this.AdShortURL);
else
adDetails = this.DataWorkspace.OtLY83U6SQ72SZCG9OtData.LSAR_AdDetails.Where(i => i.AdShortURL == this.AdShortURL&& i.Id!= this.Id);


In an entity called adDetail I have a non-identity field that should never have a duplicate value.

adShortURL.

 

I have a custom New data and a custom edit screen attached to the entity called adDetail.

 

When the new data screen is used.  The validations on the field works great.  Even going to the database to check for a duplicate value.

 

However, when I go to edit the same table using existing data, the duplicate value validation stops the screen from saving.

 

Tried to determine the screen name so put an if around the validation.  Did not find a way.  Tried to set a screen parameter so I could look for it in the validation.  that did not work.  Did not have the screen parameter available in the custom validation method. (might not know where to look).

 

Please help me figure out how to validate on add and skip the validation on edit screens.

 

Thanks,

Victor

 

Code Snippet:

  partial void AdShortURL_Validate(EntityValidationResultsBuilder results)

        {

            // results.AddPropertyError("<Error-Message>");

 

          // *** There is a Bug here that only happens on the Edit Screen.  On the edit screen, the check happens and finds a duplicate in the databae prohibiting saving.

 

                    //Hit the database and see if there is already a value in there like the one I am Entering.

                    IDataServiceQueryable<LSAR_AdDetail> adDetails =

 

                        this.DataWorkspace.OtLY83U6SQ72SZCG9OtData.LSAR_AdDetails.Where(i => i.AdShortURL == this.AdShortURL);

解决方案

I think it's because the query in the AdShortURL_Validate is finding the same entity in the data source and is failing to recognized that it is itself. This is why you do not get the error on adding a new AdDetail entity (because it doesn't exist in the data source yet) and you get the error when you are editing the existing entity.

I would suggest changing your query to account for the entity's self once it has been saved, something like:

IDataServiceQueryable<LSAR_AdDetail> adDetails = null;
if (this.Details.EntityState == EntityState.Added)
    adDetails = this.DataWorkspace.OtLY83U6SQ72SZCG9OtData.LSAR_AdDetails.Where(i => i.AdShortURL == this.AdShortURL);
else
    adDetails = this.DataWorkspace.OtLY83U6SQ72SZCG9OtData.LSAR_AdDetails.Where(i => i.AdShortURL == this.AdShortURL && i.Id != this.Id);


这篇关于实体字段上的验证将停止保存编辑屏幕。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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