为什么crm动力学模型没有属性验证?

时间:2017-03-05 02:08:15

标签: c# asp.net .net visual-studio dynamics-crm-2011

CRM有一个模型generation tool,可以在开发时使用,以便于早期绑定的使用。数据模型在此结构中定义:

/// <summary>
    /// Note that is attached to one or more objects, including other notes.
    /// </summary>
    [System.Runtime.Serialization.DataContractAttribute()]
    [Microsoft.Xrm.Sdk.Client.EntityLogicalNameAttribute("annotation")]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "5.0.9690.3339")]
    public partial class Annotation : Microsoft.Xrm.Sdk.Entity, System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
    {

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public Annotation() : 
                base(EntityLogicalName)
        {
        }

        public const string EntityLogicalName = "annotation";

        public const int EntityTypeCode = 5;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

        private void OnPropertyChanged(string propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        private void OnPropertyChanging(string propertyName)
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, new System.ComponentModel.PropertyChangingEventArgs(propertyName));
            }
        }...
...

使用非常简单:

var note = new Annotation();
note.noteText = "this is my note";
..

上面的类中的字段都有某种定义的边界。例如,int不能为负数,字符串必须小于10个字符etc

是否可以欺骗模型生成器工具在字段中包含属性?

预期的结果是在每个字段上都有一个类:

    public class Product
{
    public int Id { get; set; }

    [Required]
    [StringLength(10)]
    public string Name { get; set; }

    [Required]
    public string Description { get; set; }

    [DisplayName("Price")]
    [Required]
    [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
    public decimal UnitPrice { get; set; }
}

1 个答案:

答案 0 :(得分:4)

CrmSvcUtil允许您扩展模型,您可以通过实现其中一个接口来添加自己的属性,我相信ICodeGenerationService。您必须了解如何操作非常详细的CodeDom。