我有两张桌子:
Fatture
和FattureMail
定义如下:
身材
public partial class Fatture {
public int Id { get; set; }
public string? IdentificativoSdi { get; set; }
public string? ProgressivoInvio { get; set; }
public int RegistroFattura { get; set; }
public DateTime Data { get; set; }
public string NumeroFattura { get; set; } = null!;
public string? RagioneSociale { get; set; }
public string? Nome { get; set; }
public string? Cognome { get; set; }
public string? Provincia { get; set; }
public string? Città { get; set; }
public string? Cap { get; set; }
public string? Indirizzo { get; set; }
public string? Paese { get; set; }
public string? PartitaIva { get; set; }
public string? CodiceFiscale { get; set; }
public string? IndirizzoPec { get; set; }
public string? CodiceDestinatario { get; set; }
public string? RagioneSocialeForn { get; set; }
public string? NomeForn { get; set; }
public string? CognomeForn { get; set; }
public string? ProvinciaForn { get; set; }
public string? CittàForn { get; set; }
public string? Capforn { get; set; }
public string? IndirizzoForn { get; set; }
public string? PaeseForn { get; set; }
public string? PartitaIvaforn { get; set; }
public string? CodiceFiscaleForn { get; set; }
public string? IndirizzoPecforn { get; set; }
public string? ValMarcaDaBollo { get; set; }
public bool ApplicaContributi { get; set; }
public string? PercentualeRivalsaInps { get; set; }
public string? PercentualeCassa { get; set; }
public string? SpeseEsenti { get; set; }
public string? SpeseNonEsenti { get; set; }
public string? NoteCliente { get; set; }
public string? NoteFattura { get; set; }
public bool SentSdi { get; set; }
public int IdtermPag { get; set; }
public string? MetodoPagamento { get; set; }
public string? Beneficiario { get; set; }
public string? Iban { get; set; }
public string? Banca { get; set; }
public int? Stato { get; set; }
public string RegimeFiscale { get; set; } = null!;
public string TipoDocumento { get; set; } = null!;
public string FormatoTrasmissione { get; set; } = null!;
public string? NomeFileXml { get; set; }
public string? Email { get; set; }
public virtual ICollection<FattureMail> FattureMails { get; set; } = new List<FattureMail>();
}
FatureMail
public partial class FattureMail
{
public int Id { get; set; }
public int Idfatt { get; set; }
public string Stato { get; set; } = null!;
public DateTime? DataInvio { get; set; }
public virtual Fatture IdfattNavigation { get; set; } = null!;
}
我执行了以下命令:
Scaffold-DbContext 'Server=****;Initial Catalog=Consulens;Persist Security Info=False;User ID=*****;Password=*****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Tables Fatture, FattureMail
现在我只想从
Fatture
中检索 FattureMail
:
public static List<FattureMail> GetEmails(string stato = StatoFattura.INVIO_IN_CORSO)
{
try
{
return _context.FattureMails
.Include(i => i.IdfattNavigation)
.Where(x => x.Stato == stato)
.ToList();
}catch(Exception e)
{
return new List<FattureMail>();
}
}
如您所见,我明确包含了
IdfattNavigation
,那么为什么尽管引用的对象存在(存在 Fatture
条目,其 ID
等于 IDFatt
的 FattureMail
属性),它总是为 null?
我尝试了 Stack Overflow 上提供的几个答案,但似乎仍然不起作用。
关于模型创建
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseCollation("SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<Fatture>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK_Fatturazione");
entity.ToTable("Fatture");
entity.Property(e => e.Id).HasColumnName("ID");
entity.Property(e => e.Banca).HasMaxLength(50);
entity.Property(e => e.Beneficiario).HasMaxLength(150);
entity.Property(e => e.Cap)
.HasMaxLength(50)
.HasColumnName("CAP");
entity.Property(e => e.Capforn)
.HasMaxLength(50)
.HasColumnName("CAPForn");
entity.Property(e => e.Città).HasMaxLength(50);
entity.Property(e => e.CittàForn).HasMaxLength(50);
entity.Property(e => e.CodiceDestinatario).HasMaxLength(50);
entity.Property(e => e.CodiceFiscale).HasMaxLength(50);
entity.Property(e => e.CodiceFiscaleForn).HasMaxLength(50);
entity.Property(e => e.Cognome).HasMaxLength(50);
entity.Property(e => e.CognomeForn).HasMaxLength(50);
entity.Property(e => e.Email)
.HasMaxLength(50)
.HasColumnName("email");
entity.Property(e => e.FormatoTrasmissione).HasMaxLength(10);
entity.Property(e => e.Iban).HasMaxLength(50);
entity.Property(e => e.IdentificativoSdi)
.HasMaxLength(50)
.HasColumnName("IdentificativoSDI");
entity.Property(e => e.IdtermPag).HasColumnName("IDTermPag");
entity.Property(e => e.Indirizzo).HasMaxLength(50);
entity.Property(e => e.IndirizzoForn).HasMaxLength(50);
entity.Property(e => e.IndirizzoPec)
.HasMaxLength(50)
.HasColumnName("IndirizzoPEC");
entity.Property(e => e.IndirizzoPecforn)
.HasMaxLength(50)
.HasColumnName("IndirizzoPECForn");
entity.Property(e => e.MetodoPagamento).HasMaxLength(50);
entity.Property(e => e.Nome).HasMaxLength(50);
entity.Property(e => e.NomeFileXml).HasColumnName("NomeFileXML");
entity.Property(e => e.NomeForn).HasMaxLength(50);
entity.Property(e => e.NoteCliente).HasColumnType("text");
entity.Property(e => e.NoteFattura).HasColumnType("text");
entity.Property(e => e.NumeroFattura).HasMaxLength(50);
entity.Property(e => e.Paese).HasMaxLength(50);
entity.Property(e => e.PaeseForn).HasMaxLength(50);
entity.Property(e => e.PartitaIva)
.HasMaxLength(50)
.HasColumnName("PartitaIVA");
entity.Property(e => e.PartitaIvaforn)
.HasMaxLength(50)
.HasColumnName("PartitaIVAForn");
entity.Property(e => e.PercentualeCassa).HasMaxLength(50);
entity.Property(e => e.PercentualeRivalsaInps)
.HasMaxLength(50)
.HasColumnName("PercentualeRivalsaINPS");
entity.Property(e => e.ProgressivoInvio).HasMaxLength(10);
entity.Property(e => e.Provincia).HasMaxLength(50);
entity.Property(e => e.ProvinciaForn).HasMaxLength(50);
entity.Property(e => e.RagioneSociale).HasMaxLength(150);
entity.Property(e => e.RagioneSocialeForn).HasMaxLength(150);
entity.Property(e => e.RegimeFiscale).HasMaxLength(10);
entity.Property(e => e.SentSdi).HasColumnName("sentSDI");
entity.Property(e => e.SpeseEsenti).HasMaxLength(50);
entity.Property(e => e.SpeseNonEsenti).HasMaxLength(50);
entity.Property(e => e.TipoDocumento).HasMaxLength(10);
entity.Property(e => e.ValMarcaDaBollo).HasMaxLength(50);
});
modelBuilder.Entity<FattureMail>(entity =>
{
entity.ToTable("FattureMail");
entity.Property(e => e.Id).HasColumnName("ID");
entity.Property(e => e.Idfatt).HasColumnName("IDFatt");
entity.Property(e => e.Stato).HasMaxLength(50);
entity.HasOne(d => d.IdfattNavigation).WithMany(p => p.FattureMails)
.HasForeignKey(d => d.Idfatt)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_FattureMail_Fatture");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
答案:
由于某种原因,延迟加载在较新的.net版本中默认未激活,因此我们需要手动激活它:
Microsoft.EntityFrameworkCore.Proxies
OnConfiguring
方法中添加以下行:optionsBuilder.UseLazyLoadingProxies();
Include()