0

I cant add a controller, it says:

Unable to retrieve Metadata for "Diets.Models.Usuario" The Property "ProgramaDietaID" cannot be configurated as Navigation Property. The property must be a valid Entity and the property should have a non-abstract getter and setter. For collection Properties the type must implement ICollection where T is a valid entity type.

I don't figure out what I'm doing wrong, the roles between "Usuario" and "ProgramaDieta" entities are one-to-many.

public class Usuario
{
    [Key]
    [Required]
    public int UsuarioID { get; set; } 

    [Display(Name = "Name:")]//lo que mostrara el titulo del campo la Vista.
    [StringLength(50, ErrorMessage = "The name cannot be longer than 50 characters.")]
    [RegularExpression(@"^[a-zA-Z''-'\s]*$")]//para que acepte solo letras y no carac. alfanumericos.
    [Required]
    public string Nombre { get; set; }
    public virtual ICollection<ProgramaDieta> ProgramaDietas { get; set; }
}

public class ProgramaDieta
{
   [Key]
   public int ProgramaDietaID { get; set; }

   [ForeignKey("UsuarioID")]
   public int UsuarioID { get; set; }
   public virtual Usuario Usuario { get; set; }
}

Context Class

public class MejoraConProgramasContext:DbContext
{
    public MejoraConProgramasContext()
        : base("MejoraConProgramasContext")
    {
    }

    public DbSet<Usuario> Usuarios { get; set; }
    public DbSet<ProgramaDieta> Programas { get; set; }
}

1 Answer 1

1

The ForeignKey is improperly attached to the int UsuarioID property instead of the virtual Usuario Usuario.

Sign up to request clarification or add additional context in comments.

3 Comments

Hehe, nice catch. I kept staring at this.
I was staring for some time, too :)
Thanks for the try, but the problem keeps, after delete Foreign key form UsuarioID and put in Usuario Usuario :(, I believe is a mapping entities problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.