微风验证实体及其导航属性 [英] Breeze Validate Entity and its navigational properties

查看:85
本文介绍了微风验证实体及其导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用代码entity.entityAspect.validateEntity()验证实体.但是,这不会验证导航属性.我的整个实体与一对一实体具有一对一和一对多的关系.我想验证实体及其导航属性.我怎样才能轻而易举地做到这一点?

I am able to validate an entity using the code entity.entityAspect.validateEntity(). However, this does not validate navigation properties. My entiy has one-to-one and one-to-many relationship with out entities. I want to validate both the entity and its navigational properties. How can i do this with breeze?

编辑 我上课

public class ClassA{
 public int id{get; set;}
 public List<ClassB> navigationArray{get; set;}
}


public class ClassB{
 public int myClass {get; set;}

 [Foreign("myClass")]
 public ClassA ClassA_E{get; set;}
}

我将ClassA的对象O1添加到实体管理器;并将classB的对象O2添加到实体管理器,并将属性ClassA_E设置为O1.一切正常,但是在验证O1时,O2无法得到验证

I add an object O1 of ClassA to the entity manager; and add an object O2 of classB to the entity manager and set the property, ClassA_E to O1. All works well but when validating O1, O2 does not get validated

推荐答案

EntityAspect.validateEntity将验证导航属性(下面的代码在1.4.17的微风中进行了测试.)

EntityAspect.validateEntity WILL validate navigation properties ( The code below was tested in breeze 1.4.17).

您可以将自己的验证器添加到任何导航属性:在以下示例中,假设架构具有客户"和订单"实体类型,其中每个客户都有一个非标量的订单"属性,每个订单"都有一个标量客户"属性.

You can add your own validators to any navigation property: In the examples below assume a schema with "Customer" and "Order" entity types where each Customer has an nonscalar "orders" property and each "Order" has a scalar "customer" property.

在这种情况下,订单"类型上的标量客户"导航属性可能注册了一个验证器,如下所示:

In this case, the scalar "customer" navigation property on the Order type might have a validator registered like this:

var orderType = em.metadataStore.getEntityType("Order");
var custProp = orderType.getProperty("customer");
// validator that insures that you can only have customers located in 'Oakland' 
var valFn = function (v) {
      // v is a customer object
      if (v == null) return true;
      var city = v.getProperty("city");
      return city === "Oakland";
};
var customerValidator = new Validator("customerValidator", valFn, { messageTemplate: "This customer's must be located in Oakland" });
custProp.validators.push(customerValidator);

将通过调用在其中创建验证错误的地方

where the validation error would be created by calling

myOrder.entityAspect.validateEntity();

客户"类型上的非标量导航属性订单"可能注册了一个验证器,如下所示:

And the nonscalar navigation property "orders" on the "Customer" type might have a validator registered like this:

var customerType = em.metadataStore.getEntityType("Customer");
var ordersProp = customerType.getProperty("orders");
// create a validator that insures that all orders on a customer have a freight cost > $100
var valFn = function (v) {
    // v will be a list of orders
    if (v.length == 0) return true; // ok if no orders
    return v.every(function(order) {
        var freight = order.getProperty("freight");
        return freight > 100;
    });
};
var ordersValidator = new Validator("ordersValidator", valFn, { messageTemplate: "All of the orders for this customer must have a freight cost > 100" });
ordersProp.validators.push(ordersValidator);

将通过调用在其中创建验证错误的地方

where the validation error would be created by calling

myCustomer.entityAspect.validateEntity();

这篇关于微风验证实体及其导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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