【问题标题】:Issue with Foreign keys and columns with Entity Framework实体框架的外键和列问题
【发布时间】:2020-09-26 15:12:48
【问题描述】:

我正在尝试使用数据注释使用实体框架创建外键,但是根据迁移信息,EF 将创建所有必需的列,但是具有外键的列并没有像我想要的那样显示出来,另外是创建一个额外的普通列。

这是我用来创建表的类。

public class Productos
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int IdProducto { get; set; }

    [Required]
    [MaxLength(100)]
    public string NombreProducto { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(MAX)")]
    public string DescripcionProducto { get; set; }

    [MaxLength(50)]
    public string Tallas { get; set; }

    [MaxLength(100)]
    public string Colores { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(MAX)")]
    public string ImagenURL { get; set; }

    [Required]
    public int Precio { get; set; }

    [Required]
    public int Inventario { get; set; }

    public int IdCategoria { get; set; }
    public virtual Categorias Categorias { get; set; }

    public virtual ICollection<DetallesCarritos> DetallesCarritos { get; set; }

    public virtual ICollection<DetallesListasDeseos> DetallesListasDeseos { get; set; }
}

从上面的代码中,我想要的外键属性是IdCategoria,它的值来自另一个类:

public class Categorias
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int IdCategoria { get; set; }
              
    [Required]
    [MaxLength(50)]
    public string NombreCategoria { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(MAX)")]
    public string DescripcionCategoria { get; set; }

    public virtual ICollection<Productos> Productos { get; set; }
}

现在,EF 迁移信息的片段:

migrationBuilder.CreateTable(
            name: "Productos",
            columns: table => new
            {
                IdProducto = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                NombreProducto = table.Column<string>(maxLength: 100, nullable: false),
                DescripcionProducto = table.Column<string>(type: "nvarchar(MAX)", nullable: false),
                Tallas = table.Column<string>(maxLength: 50, nullable: true),
                Colores = table.Column<string>(maxLength: 100, nullable: true),
                ImagenURL = table.Column<string>(type: "nvarchar(MAX)", nullable: false),
                Precio = table.Column<int>(nullable: false),
                Inventario = table.Column<int>(nullable: false),
                IdCategoria = table.Column<int>(nullable: false),
                CategoriasIdCategoria = table.Column<int>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Productos", x => x.IdProducto);
                table.ForeignKey(
                    name: "FK_Productos_Categorias_CategoriasIdCategoria",
                    column: x => x.CategoriasIdCategoria,
                    principalTable: "Categorias",
                    principalColumn: "IdCategoria",
                    onDelete: ReferentialAction.Restrict);
            });

从上面的代码中,对于Productos 表,EF 将创建列IdCategoria 作为普通列,但我想要这个带有外键的列。额外的列 CategoriasIdCategoria 是 EF 使用外键创建的实际列。

我试过了:

    [ForeignKey("IdCategoria")]
    public int IdCategoria { get; set; }
    public virtual Categorias Categorias { get; set; }

但我得到了相同的结果。

我正在使用 EF Core,而且我是新手,因此我们不胜感激。对不起我的英语。

【问题讨论】:

  • 您需要输入[ForeignKey("Categorias")]。你能检查一下这是否有效吗?详情请查看this

标签: c# entity-framework-core


【解决方案1】:

您将数据注释定位在错误的位置,更正您的代码如下:

public int IdCategoria { get; set; }

[ForeignKey("IdCategoria")]
public virtual Categorias Categorias { get; set; }

也可以使用表名引入外键:

[ForeignKey("Categorias")]
public int IdCategoria { get; set; }

public virtual Categorias Categorias { get; set; }

为了更深入,您可以关注this 链接。 祝你好运。

【讨论】:

  • 谢谢,我用的是第二种方法。它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
相关资源
最近更新 更多