How to: Apply Constraints to Entity Fields

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

With LightSwitch, you can add constraints to the fields of your data entity, such as specifying minimum and maximum values. These constraints are checked every time that a user creates or modifies an instance of an entity.

For example, suppose your application contains a Products entity that has a UnitPrice field. Because you do not want a user to enter a negative unit price, you add a constraint to make sure that the unit price entered is positive. If the user attempts to enter a negative value, the field becomes red and an error message appears.

To add a constraint to a field

  1. Open the entity that contains the field that you want to add a constraint to by double-clicking the entity in Solution Explorer.

    The Entity Designer opens.

  2. From the Entity Designer, select the field that you want to add a constraint to. Then, find the Validation section of the Properties window. The constraints that are available depend on the type of the selected field:

    Constraint

    Description

    Applies To

    Is Required

    Specifies whether the field must have a value. If a value is required, select this checkbox.

    Binary, Boolean, DateTime, Decimal, Double, EmailAddress, Image, Int16, Int32, Int64, Money, PhoneNumber, String

    Maximum Length

    Specifies the maximum length of the field in characters.

    Binary, EmailAddress, Image, PhoneNumber, String

    Maximum Value

    Specifies the maximum value of the field.

    DateTime, Decimal, Double, Int16, Int32, Int64, Money

    Minimum Value

    Specifies the minimum value of the field.

    DateTime, Decimal, Double, Int16, Int32, Int64, Money

    Precision

    Specifies the maximum number of digits for the value of the field. This includes digits both left of and right of the decimal point.

    Decimal, Money

    Scale

    Specifies the maximum number of digits to the right of the decimal point for the value of the field.

    Decimal, Money

    Select the constraint that you want to add to the current field, and type its value in the correct textbox. For example, to specify that a value must be positive, type 0 in the Minimum Value textbox.

    Now, any time a user attempts to enter a value that violates one of the predefined constraints, the field becomes red and an error message appears. The user cannot save the data until the error is fixed.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft