【发布时间】:2013-01-27 01:04:57
【问题描述】:
我有这个简单的模型
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; }
}
和
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;
}
用法:
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);
}
}
当我尝试为 Livro 类创建强类型控制器时,我得到了这个:
【问题讨论】:
-
请在焦点消息框中按 Ctrl+C 并将错误消息作为文本粘贴到您的问题中。
标签: c# .net entity-framework ef-code-first