代码优先 属性 '' 类型 '' 上的 ForeignKeyAttribute 无效。 Entity Framework
Code First The ForeignKeyAttribute on property '' on type '' is not valid. Entity Framework
我正在尝试创建代码,首先接近具有不同外键的用户 table,但它给我错误消息:
"The ForeignKeyAttribute on property 'discount_id' on type 'EFNetflixAssignment.User' is not valid. The foreign key name 'Discounts' was not found on the dependent type 'EFNetflixAssignment.Discount'. The Name value should be a comma separated list of foreign key property names."
这是我的用户代码 table
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]
public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }
[Required]
public virtual List<Payment_type> Payment_Types { get; set; }
[Required]
public virtual bool Activated { get; set; }
[Required]
[ForeignKey("Discounts")]
public virtual List<Discount> discount_id { get; set; }
}
这里是折扣码
public class Discount
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
[MaxLength(255)]
public virtual string discount_type { get; set; }
[Required]
public virtual decimal discount_amount { get; set; }
}
有人可以帮我解决这个问题吗?
ForeignKey-Attribute 不会像这样工作。
如果你不关心数据库中外键的名字,那么你就不需要这个属性。 EF 将自动为导航属性创建一个外键。
如果要指定此键的名称,则需要用户 class 中的另一个 属性,它保存外键,然后您可以将这 2 个属性连接成 2 种可能方式:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]
public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }
[Required]
public virtual List<Payment_type> Payment_Types { get; set; }
[Required]
public virtual bool Activated { get; set; }
[Required]
[ForeignKey("Discounts")]
public List<int> Discount_Ids { get; set; }
[Required]
public virtual List<Discount> Discounts { get; set; }
或者像这样:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]
public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }
[Required]
public virtual List<Payment_type> Payment_Types { get; set; }
[Required]
public virtual bool Activated { get; set; }
[Required]
public List<int> Discount_Ids { get; set; }
[Required]
[ForeignKey("Discount_Ids")]
public virtual List<Discount> Discounts { get; set; }
这是因为每当您想使用数据注释为外键设置特定名称时,您需要为该键添加一个 属性 并将其与导航连接 属性.
但请注意,在您的数据库中,外键将设置为折扣 table,因为在一对多关系中,table 对多- side 始终将 1-side 的主键作为关系的外键。
希望这能解决您的问题,如果您还有其他问题,请告诉我:)
我正在尝试创建代码,首先接近具有不同外键的用户 table,但它给我错误消息:
"The ForeignKeyAttribute on property 'discount_id' on type 'EFNetflixAssignment.User' is not valid. The foreign key name 'Discounts' was not found on the dependent type 'EFNetflixAssignment.Discount'. The Name value should be a comma separated list of foreign key property names."
这是我的用户代码 table
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]
public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }
[Required]
public virtual List<Payment_type> Payment_Types { get; set; }
[Required]
public virtual bool Activated { get; set; }
[Required]
[ForeignKey("Discounts")]
public virtual List<Discount> discount_id { get; set; }
}
这里是折扣码
public class Discount
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
[MaxLength(255)]
public virtual string discount_type { get; set; }
[Required]
public virtual decimal discount_amount { get; set; }
}
有人可以帮我解决这个问题吗?
ForeignKey-Attribute 不会像这样工作。 如果你不关心数据库中外键的名字,那么你就不需要这个属性。 EF 将自动为导航属性创建一个外键。
如果要指定此键的名称,则需要用户 class 中的另一个 属性,它保存外键,然后您可以将这 2 个属性连接成 2 种可能方式:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]
public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }
[Required]
public virtual List<Payment_type> Payment_Types { get; set; }
[Required]
public virtual bool Activated { get; set; }
[Required]
[ForeignKey("Discounts")]
public List<int> Discount_Ids { get; set; }
[Required]
public virtual List<Discount> Discounts { get; set; }
或者像这样:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }
[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]
public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }
[Required]
public virtual List<Payment_type> Payment_Types { get; set; }
[Required]
public virtual bool Activated { get; set; }
[Required]
public List<int> Discount_Ids { get; set; }
[Required]
[ForeignKey("Discount_Ids")]
public virtual List<Discount> Discounts { get; set; }
这是因为每当您想使用数据注释为外键设置特定名称时,您需要为该键添加一个 属性 并将其与导航连接 属性.
但请注意,在您的数据库中,外键将设置为折扣 table,因为在一对多关系中,table 对多- side 始终将 1-side 的主键作为关系的外键。
希望这能解决您的问题,如果您还有其他问题,请告诉我:)