【问题标题】:How do I create a unique field in MVC Model?如何在 MVC 模型中创建唯一字段?
【发布时间】:2013-10-21 06:27:33
【问题描述】:

这是我在 C# MVC 项目中的模型类。我想将会员 ID 设置为唯一字段?

public class Customer
{
        public int CustomerID { get; set; }
        public string MembershipID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
}

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-5


【解决方案1】:

实体框架不支持属性的唯一性,因此我们必须为此创建一个函数。 建议你用FluentValidation,很容易上手

public class Customer
{
        public int CustomerID { get; set; }
        public string MembershipID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
}

你可以通过继承来为这个类定义一组验证规则

    using FluentValidation;

    public class CustomerValidator : AbstractValidator<Customer> {

    public CustomerValidator() {
        RuleFor(customer => customer.EmailAddress).NotNull().Must(UniqueAddmail);
      }

   public bool UniqueAddmail (string EmailAddress)
        {
            return new AnnuaireDbEntities().Wilaya.FirstOrDefault(x => x.Nom == EmailAddress) == null;
        }
    }

【讨论】:

    【解决方案2】:

    我只能猜测,因为你没有提到你班上的钥匙是什么。

    根据 DataAnnotations,Entity Framework 不支持 unique 列。您唯一能做的就是将一列设置为主键,并将另一列作为 Guid 的 ID 列。

    假设 CustomerID 是您的主键:

    using System.ComponentModel.DataAnnotations;
    
    public class Customer
    {
        [Key]
        public int CustomerID { get; set; }
        public Guid MembershipID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
    }
    

    假设您的 MembershipID 是您的主键:

    using System.ComponentModel.DataAnnotations;
    
    public class Customer
    {
    
        public Guid CustomerID { get; set; }
        [Key]
        public int MembershipID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
    }
    

    通过将其中一列设为 guid,您生成 2 个具有相同值的键的机会几乎为零。

    【讨论】:

    • 您能告诉我如何在 Context 类中添加唯一列吗?因为我需要以某种方式将会员 ID 设置为唯一的。
    • 哪个context 类? DbContext?
    【解决方案3】:
    public class Customer
    {
        public int CustomerId { get; set; }
        public string MembershipId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
    }
    
    public class Company
     {
         public string MembershipId { get; set; }
         public string Name {get; set;}
    
     }
    public class CustomersBankAccount
     {
       public string Name {get; set;}
       public int CustomerId { get; set; }
     }
    

    当您想要使用代码优先约定构建数据库时,约定是在您的属性中集成一个“Id”(外键)部分。 当然,我建议将所有属性都声明为虚拟,以便 EF 可以掌握您的代码并启用某些功能。就像一个高效的跟踪机制

    所以:如果您在名为 ID 的对象上有一个属性,则 EF 假定该属性 保存主键值并在 SQL Server 中设置自动递增(标识)键列 保存属性值。

    编辑:

    “在对象上”是指客户的对象。本质上,您的示例中的主键在 customer 中,外键在与 customer 相关的所有其他表中。主键不能重复或为空。外键 - 是的。同一客户可能有多个银行账户。 :) 因此,如果您想知道设置钥匙的管道在哪里,请停下来。 MVC 可以只查看上面的代码并决定(在脚手架选择上)如何在数据库中制作表格。除了您已经看到的内容(“Id”和结尾)之外,您无需在代码中指明任何内容

    根据我的评论进行编辑:

      public class Customer
    {
        public int CustomerId { get; set; }
        [Remote("CheckMembershipIdActionMethod", "CorrespondingController")]
        public string MembershipId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
    }
    

    【讨论】:

    • 如果您建议属性是虚拟的,为什么不举个例子,将Customer 类中的MembershipId 设为虚拟?
    • @Serv 虚拟属性不是必需的,但它们确实让 EF 控制构建它自己的机制
    • CustomerId 是主键。会员 ID 是唯一的字段,如国民身份证号码
    • @user2901979 好的。在这种情况下,您可以使用远程属性。它的工作是确保您的功能得到满足。 msdn.microsoft.com/en-us/library/gg508808%28v=vs.98%29.aspx
    猜你喜欢
    • 2021-04-30
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 2013-07-05
    相关资源
    最近更新 更多