MVC Core 2.1:找到了多个相同类型的自定义属性

时间:2018-07-26 20:56:55

标签: asp.net-mvc asp.net-core .net-core

我写了一个模型。我去搭建控制器并收到以下错误:

找到了多个相同类型的自定义属性。

我看了十二遍模型,但没有看到任何重复的属性。我不确定这是怎么回事。

using Supplies.Web.Resources;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Supplies.Web.Models
{
    public class Inventory
    {
        public Inventory()
        {
            this.IsActive = true;
        }

        [Column(TypeName = "int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        [ScaffoldColumn(false)]
        public int Id { get; set; }

        [Display(AutoGenerateField = false, AutoGenerateFilter = false, Description = "Is active.", Name = "IsActive", Prompt = "IsActive", ResourceType = typeof(PropNames), ShortName = "Is Active")]
        [Required(AllowEmptyStrings = false, ErrorMessage = null, ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(SysMsg))]
        public bool IsActive { get; set; }

        [Column(TypeName = "varchar(250)")]
        [DataType(DataType.Text)]
        [Display(AutoGenerateField = false, AutoGenerateFilter = false, Description = "Item description.", Name = "ItemDescription", Prompt = "ItemDescription", ResourceType = typeof(PropNames), ShortName = "Item Description")]
        [MaxLength(250)]
        [MinLength(10)]
        [Required(AllowEmptyStrings = false, ErrorMessage = null, ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(SysMsg))]
        [StringLength(250, ErrorMessage = null, ErrorMessageResourceName = "LengthBetween", ErrorMessageResourceType = typeof(SysMsg), MinimumLength = 10)]
        public string ItemDescription { get; set; }

        [Column(TypeName = "varchar(50)")]
        [DataType(DataType.Text)]
        [Display(AutoGenerateField = false, AutoGenerateFilter = false, Description = "MFG.", Name = "MFG", Prompt = "MFG", ResourceType = typeof(PropNames), ShortName = "MFG")]
        [MaxLength(50)]
        [MinLength(2)]
        [Required(AllowEmptyStrings = false, ErrorMessage = null, ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(SysMsg))]
        [StringLength(50, ErrorMessage = null, ErrorMessageResourceName = "LengthBetween", ErrorMessageResourceType = typeof(SysMsg), MinimumLength = 2)]
        public string MFG { get; set; }

        [Column(TypeName = "int")]
        [Display(AutoGenerateField = false, AutoGenerateFilter = false, Description = "Reorder level.", Name = "ReorderLevel", Prompt = "ReorderLevel", ResourceType = typeof(PropNames), ShortName = "Reorder Level")]
        [Required(AllowEmptyStrings = false, ErrorMessage = null, ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(SysMsg))]
        public int ReorderLevel { get; set; }

        [Column(TypeName = "int")]
        [Display(AutoGenerateField = false, AutoGenerateFilter = false, Description = "Reorder quantity.", Name = "ReorderQuantity", Prompt = "ReorderQuantity", ResourceType = typeof(PropNames), ShortName = "Reorder Quantity")]
        [Required(AllowEmptyStrings = false, ErrorMessage = null, ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(SysMsg))]
        public int ReorderQuantity { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

我正在四处搜寻,发现可能有用的东西,现在可以使用了。

您似乎无法同时使用[DataType(DataType.EmailAddress)]和[EmailAddress]。

类似地,[DataType(DataType.PhoneNumber)]和[Phone]也是如此

您可以在EF6中执行此操作,但是Core似乎不喜欢它吗?真奇怪。

[Column(TypeName = "varchar(128)")]
[DataType(DataType.EmailAddress)]
[Display(AutoGenerateField = false, AutoGenerateFilter = false, Description = "Email address.", Name = "EmailAddress", Prompt = "EmailAddress", ResourceType = typeof(PropNames), ShortName = "Email Address")]
//[EmailAddress]
[MaxLength(128)]
[MinLength(6)]
[Required(AllowEmptyStrings = false, ErrorMessage = null, ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(SysMsg))]
[StringLength(128, ErrorMessage = null, ErrorMessageResourceName = "LengthBetween", ErrorMessageResourceType = typeof(SysMsg), MinimumLength = 6)]
public string EmailAddress { get; set; }