Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF包含列表始终为空_C#_Postgresql_Entity Framework_Asp.net Core - Fatal编程技术网

C# EF包含列表始终为空

C# EF包含列表始终为空,c#,postgresql,entity-framework,asp.net-core,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; s

由于某些原因,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; }
}
我的包含列表始终为空

我想这是一些明显的原因,为什么这不起作用,但我就是不明白

谢谢

编辑: 以下是sql查询:

从“public\”、“searchprofilepush\”中选择“Extent1\”、“id\”、“Extent1\”、“accountid\”、“push\”、“Extent1\”、“searchprofileid\”、“searchprofilepush\”作为“Extent1\”,其中“Extent1\”、“accountid\”=@p\uu linq\uu 0和@p\uu linq\uu 0不为空,且(“extentt1\”searchprofileid\”searchprofileid\“=@p\uq\uu linu\uu\uu 1和@p\uq不为空)限制1

编辑2: 现在,我已经双向映射了实体,列表仍然始终为空

编辑3:

这就是我创建数据库表的方式。

我阅读的加载相关实体的文档与示例代码和您的代码有些不同

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

public virtual ICollection<SearchProfileMediaTypePush> SearchProfileMediaTypePush { get; set; }
因此,您可以尝试:

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

您能做这两个更改,然后再试一次吗?

这里有一些问题。没有为关系定义关键点或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; }
}
[表(“searchprofilepush”)]
公共类搜索推送
{
[关键]
公共int Id{get;set;}
public int AccountId{get;set;}
公共布尔推送{get;set;}
public int UserPushId{get;set;}
public UserPush UserPush{get;set;}
public int SearchProfileId{get;set;}
公共SearchProfile SearchProfile{get;set;}
公共ICollection SearchProfileMediaTypePush{get;set;}
}
[表(“searchprofilemediatypepush”)]
公共类SearchProfileMediaTypePush
{
[关键]
公共int Id{get;set;}
公共媒体类型媒体类型{get;set;}
公共布尔推送{get;set;}
public int SearchProfilePushId{get;set;}
[外键(“SearchProfilePushId”)]
public SearchProfilePush SearchProfilePush{get;set;}
}
就我个人而言,我更喜欢使用EntityTypeConfiguration类显式地映射关系,但也可以在上下文的OnModelCreating中设置它们。作为起点,请查看基本EF关系配置

对于SearchProfilePush配置:

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

如何映射此导航属性?生成的SQL查询是什么?我添加了一个编辑。似乎它根本没有加入:(@devilsuichroso导航属性是如何映射的?我不认为是这样的。virtual关键字是如果你想懒洋洋地加载实体,我不想这样做。在第一个或默认值之前设置一个where只会使它抓取一个列表,然后在该列表中取第一个,而不只是在之前限制它。关于
virtual
关键字:在我看来,如果使用
,相关的实体似乎不是延迟加载的。Include
外键是按约定建立的。除非不想遵循通常的EF约定,否则不必显式指定它们。关于更新的示例:删除其中一个映射。即,使用.HasMany()。WithRequired()在父实体上,删除子实体上的.HasRequired().WithMany()。我看到了双重映射的问题。此外,鉴于PK上的序列类型,您可能还应该将PK字段的DatabaseGeneratedOption设置为“Identity”。
[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; }
}
modelBuilder.Entity<SearchProfilePush>()
  .HasMany(x => x.SearchProfileMediaTypePush)
  .WithRequired(x => x.SearchProfilePush)
  .HasForeignKey(x => x.SearchProfilePushId);