我试图获取一个包含相关表Adress的完全填充的User对象,然后包含Plz_Ort。
public partial class User
{
//No hash sets for Adress Scaffolded (probably unnecessary since the is
//only ever one address per user
public int IdUser { get; set; }
public int? FidAdresse { get; set; }
public string Email { get; set; }
public string Vorname { get; set; }
public string Nachmname { get; set; }
[ForeignKey("FidAdresse")]
[InverseProperty("User")]
public virtual Adresse FidAdresseNavigation { get; set; }
}
public partial class Adresse
{
public Adresse()
{
User = new HashSet<User>();
}
public int IdAdresse { get; set; }
public int FidPlzOrt { get; set; }
public string Strasse { get; set; }
public string Hausnr { get; set; }
[ForeignKey("FidPlzOrt")]
[InverseProperty("Adresse")]
public virtual PlzOrt FidPlzOrtNavigation { get; set; }
[InverseProperty("FidAdresseNavigation")]
public virtual ICollection<User> User { get; set; }
}
public partial class PlzOrt
{
public PlzOrt()
{
Adresse = new HashSet<Adresse>();
}
public int IdPlzOrt { get; set; }
public string Plz { get; set; }
public string Ort { get; set; }
[InverseProperty("FidPlzOrtNavigation")]
public virtual ICollection<Adresse> Adresse { get; set; }
}
关系如下:用户1:1地址和地址n:1 Plz_ort。
然后我进行一个linq查询,并抛出异常:'EF.Property方法只能在LINQ查询中使用。'
return _context.User
.Include(u => u.FidPermissionNavigation)
.Include(c => c.FidAdresseNavigation)
.ThenInclude(c => c.FidPlzOrtNavigation)
.FirstOrDefault(c => c.IdUser == id);
当我注释掉“ theninclude(c => c.FidPlzOrtNavigation)”时,此查询有效。我得到一个plznavigation设置为null的对象。
此查询工作正常。
return _context.Adresse
.Include(c => c.FidPlzOrtNavigation)
.FirstOrDefault(c => c.IdAdresse == idAddresse);
有什么办法解决此问题吗?还是我通常不应该尝试查询具有大量相关数据的对象?
更新
我将所有这些表DB First与Pomelo一起使用。显然,脚手架忽略了我所有的1:1关系,并决定将它们全部设为1:n。这是dbcontext生成的代码。
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(e => e.IdUser)
.HasName("PRIMARY");
entity.HasIndex(e => e.FidAdresse)
.HasName("fk_User_Adresse1_idx");
entity.HasOne(d => d.FidAdresseNavigation)
.WithMany(p => p.User)
.HasForeignKey(d => d.FidAdresse)
.HasConstraintName("fk_User_Adresse1");
});
modelBuilder.Entity<Adresse>(entity =>
{
entity.HasKey(e => e.IdAdresse)
.HasName("PRIMARY");
entity.HasIndex(e => e.FidPlzOrt)
.HasName("fk_Adresse_plz_ort1_idx");
entity.HasOne(d => d.FidPlzOrtNavigation)
.WithMany(p => p.Adresse)
.HasForeignKey(d => d.FidPlzOrt)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_Adresse_plz_ort1");
});
modelBuilder.Entity<PlzOrt>(entity =>
{
entity.HasKey(e => e.IdPlzOrt)
.HasName("PRIMARY");
});
万一脚手架在实体上创建了错误的关系,我将把该帖子更新为新标题并刷新该帖子。
我所有的1:1关系都被定义为无法识别 如何正确地使Pomelo与支架建立1:1关系?我尝试删除并添加用户和地址之间的1:1关系,并将EER的更改与数据库同步。之后,我用--force进行了更改,因此实体将被覆盖,但是DBContext仍然根本没有更改实体。