【问题标题】:Why doesn't crm dynamics model have attribute validations?为什么 crm 动力学模型没有属性验证?
【发布时间】:2017-03-05 02:08:14
【问题描述】:

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";
..

以上类的字段都有某种定义的界限。例如整数不能为负数,字符串需要小于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; }
}

【问题讨论】:

  • 看看crmcodegenerator.codeplex.com。我与这个项目没有关系,也没有使用它,但我一直很想看到 CrmSvcUtil.exe 被 T4 模板取代(对我来说,这是在 .NET/Visual Studio 世界中进行代码生成的正确方法.
  • @Nicknow 请将其作为答案

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


【解决方案1】:

CrmSvcUtil 允许您扩展模型,并且您可以添加自己的属性,通过实现它们的接口之一,我相信 ICodeGenerationService。您必须了解如何操作非常冗长的 CodeDom。

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 2014-03-01
    • 2020-12-13
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多