繁体   English   中英

每个表中的列名必须唯一。 表Entity1'中的列名'Id'被多次指定

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

想法是将nameof用作外键,因为如果更改属性名的编译器会捕获到此,则我将永远不会忘记更改外键依赖项

public class Entity1 {
    [Key]
    public int Id { get; set; }
    public int OtherId { get; set; }

    [Foreign Key(nameof(OtherId))]
    public virtual Entity2 Entity { get; set; }
}
   public Entity2 {
    [Key]
    public int Id {get;set;}
    public string Name {get;set;}
}

当我尝试从迁移中更新数据库时,出现主题错误。 我在这里做错什么了吗? 问候,

您已经在该类的同一个类中拥有一个属性。 尝试更改外键名称,例如:

[ForeignKey("name of foreign key here")]
  public virtual Entity2 Entity { get; set; }

看到:

https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

你可以试试这个

public class Entity1 
{
  [Key]
  public int Id { get; set; }

  [ForeignKey("Entity2")]
  public int Entity2ID { get; set; }

  public virtual Entity2 Entity2 { get; set; }
}

暂无
暂无

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

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