C# 如何在MVC模型中创建唯一字段?

C# 如何在MVC模型中创建唯一字段?,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,这是我在C#MVC项目中的模型课。我想将成员身份设置为唯一字段 public class Customer { public int CustomerID { get; set; } public string MembershipID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } pu

这是我在C#MVC项目中的模型课。我想将成员身份设置为唯一字段

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; }
}

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

根据DataAnnotations,实体框架不支持
唯一的
列。您唯一能做的就是将一列设置为主键,并将另一个ID列设置为Guid

假设
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; }
}
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; }
}
假设您的MembershipID是您的主键:

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; }
}
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,生成相同值的两个键的几率接近于零

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可以查看上面的代码并决定(在脚手架选择上)如何在DB中创建选项卡。除了allready看到的内容外,您不必在代码中指出任何内容(“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; }
}

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

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;
        }
    }
使用FluentValidation;
公共类CustomerValidator:AbstractValidator{
公共CustomerValidator(){
RuleFor(customer=>customer.EmailAddress).NotNull()必须(UniqueAddmail);
}
public bool UniqueAddmail(字符串EmailAddress)
{
返回新的AnnualEdBenties().Wilaya.FirstOrDefault(x=>x.Nom==EmailAddress)==null;
}
}

此对象是否映射到某个ORM,如EF?是的,确实是您想要的。这是EF MVC项目如果您建议属性是虚拟的,为什么不在
客户
类中设置
成员身份ID
作为示例?@Serv虚拟属性不是必需的,但他们确实让EF在建立自己的机制方面拥有控制权,而施托默德是主要的关键。会员身份证是一个独特的领域,就像国家身份证一样number@user2901979好啊在这种情况下,可以使用“远程”属性。它的工作是确保你的职能得到履行。您能告诉我如何在上下文类中添加唯一列吗??因为我需要将成员Id设置为唯一的。如何设置。哪个
context
类?数据库上下文?