【问题标题】:Entity Framework Code First not working properly实体框架代码首先无法正常工作
【发布时间】: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


【解决方案1】:

我相信这是因为 AutorID 属性不是公开的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2011-07-29
    • 1970-01-01
    • 2011-08-23
    相关资源
    最近更新 更多