.net core 自动加载到.net core 2.2中的相关数据
我首先使用带有实体框架代码的.net core 2.2,我创建了一些具有关系的模型,所有关系都工作良好,除非与查找模型的关系也自动加载模型的相关数据和反向属性 代码如下: Company.cs.net core 自动加载到.net core 2.2中的相关数据,.net-core,asp.net-core-mvc,entity-framework-core,asp.net-core-webapi,code-first,.net Core,Asp.net Core Mvc,Entity Framework Core,Asp.net Core Webapi,Code First,我首先使用带有实体框架代码的.net core 2.2,我创建了一些具有关系的模型,所有关系都工作良好,除非与查找模型的关系也自动加载模型的相关数据和反向属性 代码如下: Company.cs public class Company { public Company() { Lookups = new HashSet<Lookup>(); } public Guid Id { get; set; } //
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; }
}
上市公司
{
上市公司()
{
Lookups=newhashset();
}
公共Guid Id{get;set;}
//关系
//地址
[ForeignKey(“AddressForeignKey”)]
公共广播地址{get;set;}
//使用者
[反向财产(“公司”)]
公共列表用户公司{get;set;}
//作为一个国家
公共Guid?CountryId{get;set;}
公共查找国家{get;set;}
//作为银行查找
公共Guid?银行标识{get;set;}
公共查找DefaultBank{get;set;}
//作为socialInsuranceOffice查找
公共Guid?SocialInsuranceOfficeId{get;set;}
公共查找DefaultSocialInsuranceOffice{get;set;}
//作为货币查找
//[ForeignKey(“DefaultCurrencyForeignKey”)]
公共Guid?CurrencyId{get;set;}
公共查找货币{get;set;}
//查找所有查找类型
公共ICollection查找{get;set;}
}
地址: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; }
}
公共类地址
{
公共Guid Id{get;set;}
//关系
//公司
[反向属性(“地址”)]
上市公司{get;set;}
//作为一个省
[ForeignKey(“LookupForeignKey”)]
公共查找省份查找{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);
});
//公司和地址
modelBuilder.Entity()
.HasOne(u=>u.Address)
.有许多(r=>r.公司)
.OnDelete(DeleteBehavior.SetNull);
//地址和查找
modelBuilder.Entity()
.HasOne(a=>a.GovernorateLookup)
.WithMany(l=>l.Governments用户地址)
.OnDelete(DeleteBehavior.SetNull);
实体(com=>
{
//公司和查找默认国家/地区
com.HasOne(c=>c.Country)
.有许多(d=>d.公司国家)
.HasForeignKey(f=>f.CountryId)
.hassconstraintname(“COM\u COUNTRY\u FK”)
.OnDelete(DeleteBehavior.Restrict);
//公司和查找defaultBank
com.HasOne(c=>c.DefaultBank)
.有许多(d=>d.公司银行)
.HasForeignKey(f=>f.BankId)
.HasConstraintName(“COM\u BANK\u FK”)
.OnDelete(DeleteBehavior.Restrict);
//公司和查找defaultSocialInsuranceOffice
com.HasOne(c=>c.DefaultSocialInsuranceOffice)
.有许多(d=>d.公司社会保险办事处)
.HasForeignKey(f=>f.SocialInsuranceOfficeId)
.HasConstraintName(“COM_SOCIALINSURANCEOFFICE_FK”)
.OnDelete(DeleteBehavior.Restrict);
//公司和货币查询
com.HasOne(c=>c.Currency)
.有许多(d=>d.公司货币)
.HasForeignKey(f=>f.CurrencyId)
.HasConstraintName(“COM\u CURRENCY\u FK”)
.OnDelete(DeleteBehavior.Restrict);
//所有查找类型的公司和查找
com.HasMany(c=>c.Lookups)
.有一家(d=>d公司)
.HasForeignKey(f=>f.CompanyId)
.hassConstraintName(“COM\u ALL\u LOOKUPS\u FK”)
.OnDelete(DeleteBehavior.Cascade);
});
请注意,所有关系都运行良好,但只有与查找“I have 5 relations with the same table lookup”的关系会自动加载所有数据并反转,我需要知道发生这种情况的原因以及如何停止它
提前感谢。您的问题很难理解,但我相信这里的“问题”是EF的对象修正。我引用“问题”,因为这实际上是一个特性 EF有一个对象缓存。无论何时查询任何内容,创建的实例都存储在该对象缓存中,从而允许将来从该缓存中提取对相同实例的查询,而不必再次往返到数据库 当涉及到关系时,如果EF的对象缓存中已经存在相关实体,它将执行“修复”,在该修复中,它会自动将相关实例添加到导航属性中。同样,这是一个优化特性,因为现在不需要发出另一个查询来检索这些特性 唯一的解决方法是在查询时使用
AsNoTracking
。例如:
var companies = await _context.Companies.AsNoTracking().ToListAsync();
使用AsNoTracking
,EF不会维护对象缓存,也不会对这些实体执行更改跟踪。最后一部分很重要,因为如果您试图以任何方式修改这些实体,则需要非常小心,因为在您显式附加它们之前,EF不会意识到它们。但是,如果您只是执行读取,那么使用AsNoTracking
实际上更具性能