Reputation: 133
Can you help me?
namespace mvcAmerica.Models
{
public class ArtModels
{
[Key]
public int idArt { get; set; }
[Required(ErrorMessage="Codigo del Articulo es Requerido")]
public string co_art { get; set; }
[Required]
public string des_art { get; set; }
[Required]
public string modelo { get; set; }
[Required]
public string referencia { get; set; }
[ForeignKey("LineaModels")]
public int IdLinea { get; set; }
public LineaModels Linea { get; set; }
}
public class LineaModels
{
[Key]
public int IdLinea { get; set; }
[Required(ErrorMessage="Indique el Codigo")]
public string co_lin { get; set; }
[Required(ErrorMessage = "Indique la Descripción")]
public string des_lin { get; set; }
}
}
Error:
The ForeignKeyAttribute on property 'IdLinea' on type 'mvcAmerica.Models.ArtModels' is not valid. The navigation property 'LineaModels' was not found on the dependent type 'mvcAmerica.Models.ArtModels'. The Name value should be a valid navigation property name.
Upvotes: 1
Views: 407
Reputation: 11458
You need to change this:
[ForeignKey("LineaModels")]
public int IdLinea { get; set; }
public LineaModels Linea { get; set; }
To this:
[ForeignKey("Linea")]
public int IdLinea { get; set; }
public virtual LineaModels Linea { get; set; }
It needs to match the property name.
I've just created the following application with no problems:
public class ArtModels
{
[Key]
public int idArt { get; set; }
[Required(ErrorMessage = "Codigo del Articulo es Requerido")]
public string co_art { get; set; }
[Required]
public string des_art { get; set; }
[Required]
public string modelo { get; set; }
[Required]
public string referencia { get; set; }
[ForeignKey("Linea")]
public int IdLinea { get; set; }
public virtual LineaModels Linea { get; set; }
}
public class LineaModels
{
[Key]
public int IdLinea { get; set; }
[Required(ErrorMessage = "Indique el Codigo")]
public string co_lin { get; set; }
[Required(ErrorMessage = "Indique la Descripción")]
public string des_lin { get; set; }
}
public class AppContext : DbContext
{
public DbSet<ArtModels> ArtModelses { get; set; }
public DbSet<LineaModels> LineaModelses { get; set; }
}
So you must be missing something else?
Upvotes: 2
Reputation: 4040
You have to change this:
[ForeignKey("LineaModels")]
public int IdLinea { get; set; }
To this:
[ForeignKey("Linea")]
public int IdLinea { get; set; }
The ForeignKey
name has to be the same as your navigation property (here Linea
).
Upvotes: 1