[英]How to implement IDataErrorInfo for Validation on Entities when editing but not viewing
Using Entity Framework 6 with DB First but still relatively new to it. 将Entity Framework 6与DB First结合使用,但仍相对较新。 I am using IDataErrorInfo on the model for validation, which works fine when editing a single entity.
我在模型上使用IDataErrorInfo进行验证,在编辑单个实体时可以正常工作。 Eg Editing a single Address.
例如,编辑一个地址。
However, I noticed a severe performance hit when loading multiple entities (using LINQ). 但是,我注意到在加载多个实体(使用LINQ)时会严重降低性能。 (eg the command
myContext.Address.ToList()
when filling a datagridview was taking over a second to populate each row). (例如,在填充datagridview时,命令
myContext.Address.ToList()
花费了一秒钟来填充每一行)。 This is because for every row retrieved from the DB, the corresponding entity fires the validation / error checking code multiple times. 这是因为对于从数据库中检索到的每一行,相应的实体都会多次触发验证/错误检查代码。 I realise this is the result of binding in the
DataGridView
, not due to simply retrieving from the DB. 我意识到这是在
DataGridView
进行绑定的结果,而不是仅仅从数据库中获取的结果。
I can work around this by setting a ValidationOn
flag, set to false by default, that just returns true to all validation checks. 我可以通过设置
ValidationOn
标志(默认情况下设置为false)来解决此问题,该标志仅对所有验证检查都返回true。 That's much quicker but it still seems inefficient to run around those validation checks when there is no reason. 这要快得多,但是在没有理由的情况下运行那些验证检查似乎仍然效率低下。
Is there a way I can disable the Interface so it can be manually 'activated' when needed? 有没有一种方法可以禁用该接口,以便可以在需要时手动对其进行“激活”?
I assume it would be better to derive a class from my Model Entities and use that for implementing IDataErrorInfo but how do I define a generic class that can derive from any of my context objects? 我认为从我的模型实体派生一个类并使用它来实现IDataErrorInfo会更好,但是如何定义一个可以从我的任何上下文对象派生的泛型类呢?
I have tried creating this: 我尝试创建此:
public class ValidatingEntity : DbSet, IDataErrorInfo
private readonly DbSet _myEntity;
public ValidatingEntity(DbSet MyEntity)
{
_myEntity = MyEntity
}
}
which seems to work, but how do I know what the entity is to perform the correct validation tests. 这似乎可行,但是我如何知道实体将执行正确的验证测试。 eg How would I do this:
例如我该怎么做:
public string this[string columnName]
{
if (_myEntity is Address)
{
Address myAddress = (Address)_myEntity;
If (columnName=="AddressCode")
{
If (myAddress.AddressCode == string.empty) return "Code can not be blank";
}
}
}
The code if (_myEntity is Address)
gives the warning given expression is never of the provided (Address) type
. 如果代码
if (_myEntity is Address)
给出警告,则given expression is never of the provided (Address) type
。
In summary, 综上所述,
UPDATE 1 更新1
I believe I have solved this by creating a Common Base class and Deriving my entities from there using the instructions found here: Using A Common Base Class Across Entity Framework Database First Entities 我相信我已经解决了这一问题,方法是创建一个通用基类,并按照此处的说明从此处派生我的实体: 在整个实体框架数据库的第一实体中使用通用基类。
It has also allowed me to build my own Interface to replace IDataErrorInfo
as that as was causing my validation code to fire multiple times which I can now avoid. 它还允许我构建自己的接口来替换
IDataErrorInfo
因为这导致我的验证代码多次触发,现在可以避免了。
经过测试后,我很高兴在更新1中确定的方法就是解决方案(即基于此: https : //fairwaytech.com/2013/09/using-a-common-base-class-across-entity-框架数据库先实体/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.