我将.net core 2.2与Entity Framework Code-First一起使用,并且我创建了一些具有关系的模型,并且所有关系都运行良好,除非与查找模型的关系也自动加载了模型的相关数据和逆属性
这里是代码
Company.cs
public class Company
{
public Company()
{
Lookups = new HashSet<Lookup>();
}
public Guid Id { get; set; }
// Relationships
// Address
[ForeignKey("AddressForeignKey")]
public Address Address { get; set; }
// User
[InverseProperty("Company")]
public List<User> UsersInCompany { get; set; }
// Lookup as a country
public Guid? CountryId { get; set; }
public Lookup Country { get; set; }
// Lookup as a bank
public Guid? BankId { get; set; }
public Lookup DefaultBank { get; set; }
// Lookup as a socialInsuranceOffice
public Guid? SocialInsuranceOfficeId { get; set; }
public Lookup DefaultSocialInsuranceOffice { get; set; }
// Lookup as a currency
//[ForeignKey("DefaultCurrencyForeignKey")]
public Guid? CurrencyId { get; set; }
public Lookup Currency { get; set; }
// Lookup for all lookups types
public ICollection<Lookup> Lookups { get; set; }
}
Address.Cs
public class Address
{
public Guid Id { get; set; }
// Relationships
// Company
[InverseProperty("Address")]
public List<Company> Companies { get; set; }
// Lookup as a governorate
[ForeignKey("LookupForeignKey")]
public Lookup GovernorateLookup { get; set; }
}
DataContext.Cs
// Company and Address
modelBuilder.Entity<Company>()
.HasOne(u => u.Address)
.WithMany(r => r.Companies)
.OnDelete(DeleteBehavior.SetNull);
// Address and Lookup
modelBuilder.Entity<Address>()
.HasOne(a => a.GovernorateLookup)
.WithMany(l => l.GovernoratesUserAsAddress)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Company>(com =>
{
// Company and Lookup for defaultCountry
com.HasOne(c => c.Country)
.WithMany(d => d.CompanyCountries)
.HasForeignKey(f => f.CountryId)
.HasConstraintName("COM_COUNTRY_FK")
.OnDelete(DeleteBehavior.Restrict);
// Company and Lookup for defaultBank
com.HasOne(c => c.DefaultBank)
.WithMany(d => d.CompanyBanks)
.HasForeignKey(f => f.BankId)
.HasConstraintName("COM_BANK_FK")
.OnDelete(DeleteBehavior.Restrict);
// Company and Lookup for defaultSocialInsuranceOffice
com.HasOne(c => c.DefaultSocialInsuranceOffice)
.WithMany(d => d.CompanySocialInsuranceOffices)
.HasForeignKey(f => f.SocialInsuranceOfficeId)
.HasConstraintName("COM_SOCIALINSURANCEOFFICE_FK")
.OnDelete(DeleteBehavior.Restrict);
// Company and Lookup for currency
com.HasOne(c => c.Currency)
.WithMany(d => d.CompanyCurrencies)
.HasForeignKey(f => f.CurrencyId)
.HasConstraintName("COM_CURRENCY_FK")
.OnDelete(DeleteBehavior.Restrict);
// Company and Lookup for all Lookups types
com.HasMany(c => c.Lookups)
.WithOne(d => d.Company)
.HasForeignKey(f => f.CompanyId)
.HasConstraintName("COM_ALL_LOOKUPS_FK")
.OnDelete(DeleteBehavior.Cascade);
});
[请注意,所有关系都运行良好,但是只有具有查询的关系“我具有5个具有相同表查询的关系”会自动加载所有数据并取反,我需要知道为什么会发生这种情况以及如何停止它。
提前感谢。
您的问题很难理解,但是我认为这里的“问题”是EF的对象修复。我引用“问题”,因为这实际上是feature。
EF具有对象缓存。每当您查询任何内容时,创建的实例都会存储在该对象缓存中,从而允许将来从该缓存中提取相同实例的查询,而不必再次往返于数据库。
关于关系,如果EF在其对象缓存中已经具有相关实体,它将执行“修复”,在该过程中EF将相关实例自动添加到导航属性中。同样,这是一项优化功能,因为现在您无需发出另一个查询来检索那些查询。
唯一的解决方法是在查询时使用AsNoTracking
。例如:
var companies = await _context.Companies.AsNoTracking().ToListAsync();
使用AsNoTracking
,EF将不会维护对象缓存,也不会对这些实体执行更改跟踪。最后一部分很重要,因为如果尝试以任何方式修改这些实体,则需要[[非常,因为EF在您明确附加它们之前不会意识到它们。但是,如果您只是在进行读取,则无论如何,使用AsNoTracking
实际上会更有效。