EF包含列表始终为空

时间:2018-06-18 14:34:30

标签: c# postgresql entity-framework asp.net-core

由于某些原因,EF不会正确加载包含的列表,因此它最终会一直为空。

以下是我正在使用的实体:

    [Table("searchprofilepush")]
public class SearchProfilePush
{
    public int Id { get; set; }
    public int AccountId { get; set; }
    public bool Push { get; set; }
    public int UserPushId { get; set; }
    public UserPush UserPush { get; set; }
    public int SearchProfileId { get; set; }
    public SearchProfile SearchProfile { get; set; }
    public ICollection<SearchProfileMediaTypePush> SearchProfileMediaTypePush { get; set; }
}


[Table("searchprofilemediatypepush")]
public class SearchProfileMediaTypePush
{
    public int Id { get; set; }
    public MediaTypeType MediaType { get; set; }
    public bool Push { get; set; }
    public int SearchProfilePushId { get; set; }
    public SearchProfilePush SearchProfilePush { get; set; }
}

然后当我试图这样做时:

 var searchProfilePush = _dataContext.SearchProfilePush.Include(w => w.SearchProfileMediaTypePush).FirstOrDefault(w => w.AccountId == accountId && w.SearchProfileId == searchProfileId);

我的包含列表始终为空。

enter image description here

我想这是一个明显的原因,为什么这不起作用,但我无法理解。

谢谢!

编辑: 这是sql查询:

SELECT \“Extent1 \”。\“id \”,\“Extent1 \”。\“accountid \”,\“Extent1 \”。\“push \”,\“Extent1 \”。\“userpushid \ “,”Extent1 \“。\”searchprofileid \“FROM \”public \“。\”searchprofilepush \“AS \”Extent1 \“WHERE \”Extent1 \“。\”accountid \“= @ p__linq__0 AND @ p__linq__0 IS NOT NULL AND(\“Extent1 \”。\“searchprofileid \”= @ p__linq__1 AND @ p__linq__1 IS NOT NULL)LIMIT 1

编辑2: 我现在已经双向映射了我的实体,列表仍然是null。

enter image description here

编辑3:

这就是我创建数据库表的方法。 enter image description here

2 个答案:

答案 0 :(得分:0)

我为加载相关实体而阅读的文档与示例代码和代码存在一些差异。 https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

首先,当您定义ICollection时,没有关键字virtual:

public virtual ICollection<SearchProfileMediaTypePush> SearchProfileMediaTypePush { get; set; }

接下来,在靠近你的示例中,他们使用查询加载相关项,第一个或默认不使用布尔表达式。选择性表达式位于where子句中:

 // Load one blogs and its related posts 
    var blog1 = context.Blogs 
                        .Where(b => b.Name == "ADO.NET Blog") 
                        .Include(b => b.Posts) 
                        .FirstOrDefault(); 

所以你可以尝试:

var searchProfilePush = _dataContext.SearchProfilePush
.Where(w => w.AccountId == accountId && w.SearchProfileId == searchProfileId)
.Include(w => w.SearchProfileMediaTypePush)
.FirstOrDefault(); 

您可以进行这两项更改并重试吗?

答案 1 :(得分:0)

这里有一些问题。您没有为关系定义键或FK:

[Table("searchprofilepush")]
public class SearchProfilePush
{
   [Key]
   public int Id { get; set; }
   public int AccountId { get; set; }
   public bool Push { get; set; }
   public int UserPushId { get; set; }
   public UserPush UserPush { get; set; }
   public int SearchProfileId { get; set; }
   public SearchProfile SearchProfile { get; set; }
   public ICollection<SearchProfileMediaTypePush> SearchProfileMediaTypePush { get; set; }
}


[Table("searchprofilemediatypepush")]
public class SearchProfileMediaTypePush
{
   [Key]    
   public int Id { get; set; }
   public MediaTypeType MediaType { get; set; }
   public bool Push { get; set; }
   public int SearchProfilePushId { get; set; }
   [ForeignKey("SearchProfilePushId")]     
   public SearchProfilePush SearchProfilePush { get; set; }
}

我个人更喜欢使用EntityTypeConfiguration类显式映射关系,但也可以在Context的OnModelCreating中设置它们。作为起点,请查看基本EF关系配置的http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

用于SearchProfilePush配置:

modelBuilder.Entity<SearchProfilePush>()
  .HasMany(x => x.SearchProfileMediaTypePush)
  .WithRequired(x => x.SearchProfilePush)
  .HasForeignKey(x => x.SearchProfilePushId);