I have this simple model
public class Autor
{
[Key]
int AutorID { get; set; }
[Required]
[MaxLength(100)]
public string Nome { get; set; }
[Required]
[Display(Name = "Data de nascimento")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime Data { get; set; }
public virtual ICollection<Livro> Livros { get; set; }
}
and
public class Livro
{
public int LivroID { get; set; }
[Required(ErrorMessage = "E necessario titulo")]
[MaxLength(100, ErrorMessage = "Titulo deve ter no maximo 100 caracteres")]
public string Titulo { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime Data { get; set; }
[Required]
[Range(1, 5000, ErrorMessage = "Valor deve ser entre 1 e 5000")]
public int Paginas { get; set; }
[Required]
public int AutorID { get; set; }
[Required]
public virtual Autor Autor { get; set;
}
Usage:
public class BibliotecaContext : DbContext
{
public DbSet<Autor> Autores { get; set; }
public DbSet<Livro> Livros { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Autor>().HasMany(p => p.Livros).WithRequired(p => p.Autor);
}
}
When i try to create a controller strongly typed for the Livro class I’m getting this:

I believe it is because the AutorID Property is not public.