Code First,模型属性在获取时始终为null

时间:2014-05-04 17:37:52

标签: c# .net asp.net-mvc ef-code-first code-first

我正在使用CodeFirst并且遇到了我正试图映射0..1 - n的问题。 在模型Media中我有一个MediaCategory类型的模型,这个模型得到一个名为MediaCategory_Id的列,并在数据库中设置了Id。但是,当我从数据库MediaCategory属性中获取数据时始终为null。我的问题是我如何映射这个,以便通过数据库中的Id获取正确的数据?

我在DbContext.cs中尝试了一个lil并且没有任何工作正常。 注意我的存储库中LazyLoading设置为true,Proxy设置为false。

上下文

public class BlogContext : DbContext
{
    public BlogContext()
        : base("name=DefaultConnection")
    {
        this.Configuration.ProxyCreationEnabled = false;
        this.Database.CreateIfNotExists();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BlogContext>());
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Post>().ToTable("Post");
        modelBuilder.Entity<PostMedia>().ToTable("PostMedia");
        modelBuilder.Entity<Media>().ToTable("Media");
        modelBuilder.Entity<MediaCategory>().ToTable("MediaCategory");

        modelBuilder.Entity<Media>().HasRequired(t => t.MediaCategory);
        modelBuilder.Entity<Post>().HasMany(t => t.MediaList);

    }

}

模型

MEDIA

public class Media
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    //[Required(ErrorMessage = "Title must be given.")]
    [MaxLength(250, ErrorMessage = "Title can't exceeded 250 characters in length.")]
    public string Title { get; set; }

    [Required(ErrorMessage = "File must be given.")]
    [MaxLength(Int32.MaxValue, ErrorMessage = "File exceeded max characters in length.")]
    public string File { get; set; }

    [MaxLength(250, ErrorMessage = "Description can't exceeded 250 characters in length.")]
    public string Description { get; set; }

    //[Required(ErrorMessage = "Author must be given.")]
    [MaxLength(50, ErrorMessage = "Author can't exceeded 50 characters in length.")]
    public string Author { get; set; }

    [Required(ErrorMessage = "Created date must be given.")]
    public DateTime? Created { get; set; }

    public DateTime? Changed { get; set; }

    public MediaCategory MediaCategory { get; set; }

}

MEDIACATEGORY

public class MediaCategory
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required(ErrorMessage = "A title must be given.")]
    [MaxLength(50, ErrorMessage = "Title can't exceeded 50 characters in length.")]
    public string Title { get; set; }

    [MaxLength(250, ErrorMessage = "Description can't exceeded 250 characters in length.")]
    public string Description { get; set; }
}

解决

public virtual MediaCategory MediaCategory { get; set; } and
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true; <-- This one was on false :)

2 个答案:

答案 0 :(得分:2)

//public MediaCategory MediaCategory { get; set; }
  public virtual MediaCategory MediaCategory { get; set; }

答案 1 :(得分:2)

当您从数据库中提取数据时,除非已配置,否则您必须明确定义要包含的子对象。

例如,您可能会执行以下操作:

_context.Media.Where( <some expression> );

尝试将其更改为:

_context.Media.Where( <some expression> ).Include(m > m.MediaCategory);