CodeFirst,模型属性在获取时总是null

本文关键字:null 获取 模型 属性 CodeFirst | 更新日期: 2023-09-27 18:01:31

我正在使用CodeFirst,并在我试图映射0的问题上卡住了。1 - n。在模型Media中,我有一个MediaCategory类型的模型,这个获得一个名为MediaCategory_Id的列并在数据库中设置了一个Id。然而,当我从数据库中获取数据时,MediaCategory属性总是空的。我的问题是我如何映射这个,这样它就能通过数据库中的Id获取正确的数据?

我在DbContext.cs中试了一下,没有任何东西可以正常工作。注意,在我的存储库中,LazyLoading设置为true, Proxy设置为false。

<<p> 上下文/strong>
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);
    }
}

媒体

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 :)

CodeFirst,模型属性在获取时总是null

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

当您从数据库中获取数据时,除非已配置,否则您必须显式定义要包含哪些子对象。

例如,您可以这样做:

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

试着把它改成:

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