繁体   English   中英

每个表中的列名必须唯一。 表“ HumanCustomers”中的列名“ LastName”已多次指定

[英]Column names in each table must be unique. Column name 'LastName' in table 'HumanCustomers' is specified more than once

更新我的模型并添加数据注释之后。

然后,我执行了add-migration和update-database命令,并收到以下错误:

每个表中的列名必须唯一。 表“ HumanCustomers”中的列名“ LastName”已多次指定。

但是姓氏字段曾经使用过。

HumanCustomer类:

public class HumanCustomer
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        [Required]
        [MinLength(2)]
        [MaxLength(20)]
        public string Name
        {
            get => Name;
            set
            {
                value.TrimAndReduce();
            }
        }
        [Required]
        [MinLength(2)]
        [MaxLength(20)]
        public string LastName
        {
            get => LastName;
            set
            {
                value = value.TrimAndReduce();
            }
        }
        [NotMapped]
        public string FullName
        {
            get { return Name + LastName; }

        }
        [Required(AllowEmptyStrings = true)]
        public int GenderId { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        [DataType(DataType.EmailAddress)]
        [EmailAddress]
        public string Email { get; set; }
        public DateTime BirthDate { get; set; }
        public int IdentityTypeId { get; set; }
        public string IdentityCode { get; set; }   
        [Required]
        [ForeignKey("UserId")]
        public virtual User User { get; set; }  
        [Required]
        [ForeignKey("IdentityTypeId")]
        public virtual IdentityType IdentityType { get; set; }
        [Required]
        [ForeignKey("GenderId")]
        public virtual Gender Gender { get; set; }
    }

和迁移:

 protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "LastName",
                table: "HumanCustomers",
                maxLength: 20,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "Name",
                table: "HumanCustomers",
                maxLength: 20,
                nullable: false,
                defaultValue: "");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "LastName",
                table: "HumanCustomers");

            migrationBuilder.DropColumn(
                name: "Name",
                table: "HumanCustomers");
        }

该错误清楚地表明您的表中已经有一个名为LastName的列,并且该迁移无法应用于添加具有相同名称的新列。
要么转到数据库管理系统,然后手动删除该列,或者如果您刚切换到代码优先,并且要在下一次迁移中忽略现有模式,则删除迁移文件并使用-IgnoreChanges创建一个新文件旗:

Add-Migration migrationName –IgnoreChanges

但是,正如@Ahmad所评论的那样,EF Core不支持-IgnoreChanges ,因此,您可以做的一件事是,如果您不想手动从表中删除该列,则可以在其中注释掉Add LastName代码。 Up方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM