分离的实体或使用 'AsNoTracking' 加载的实体不支持延迟加载
Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking'
我使用 Entity Framework 作为 SQL 服务器的 ORM。
这是我里面的代码 POST API:
var group = await _context.Groups.FindAsync(id);
foreach (CalendarRuleGroup calendarRuleGroup in group.CalendarRuleGroups)
{
CalendarRule calendarRule = _context.CalendarRules
.Where(cr => cr.CalendarRuleGroupId == calendarRuleGroup.Id)
.First();
CalendarRule calendarRuleTemp = (CalendarRule)calendarRule.Clone();
_context.CalendarRules.Add(calendarRuleTemp);
}
await _context.SaveChangesAsync();
这是 Clone() 函数:
public object Clone()
{
CalendarRule clonedRule = (CalendarRule)MemberwiseClone();
clonedRule.Calendar = null;
clonedRule.CalendarId = default;
clonedRule.CalendarRuleGroup = null;
clonedRule.CalendarRuleGroupId = default;
clonedRule.CalendarContexts = null;
clonedRule.Id = default;
List<CalendarRuleCondition> conditions = new List<CalendarRuleCondition>();
foreach (CalendarRuleCondition condition in clonedRule.CalendarRuleConditions)
{
conditions.Add((CalendarRuleCondition)condition.Clone());
}
clonedRule.CalendarRuleConditions = conditions;
return clonedRule;
}
这是 CalendarRule 的模型:
public class CalendarRule: ICloneable
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime StartingFrom { get; set; }
public DateTime? Until { get; set; }
public int? Count { get; set; }
public double Duration { get; set; }
public Frequency Frequency { get; set; }
public int Interval { get; set; }
public int Priority { get; set; }
public bool IsWorking { get; set; }
public int Efficiency { get; set; }
public int Cost { get; set; }
public bool AllowedSetup { get; set; }
public int Color { get; set; }
public string Description { get; set; }
public bool? IsActive { get; set; }
public int CalendarId { get; set; }
public int? CalendarRuleGroupId { get; set; }
public virtual Calendar Calendar { get; set; }
public virtual CalendarRuleGroup CalendarRuleGroup { get; set; }
public virtual ICollection<CalendarContext> CalendarContexts { get; set; }
public virtual ICollection<CalendarRuleCondition> CalendarRuleConditions { get; set; }
}
这是foreach里面的“clonedRule.CalendarRuleConditions”执行时出现的错误:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMG79T5K5ECK", Request id "0HMG79T5K5ECK:00000002": An unhandled exception was thrown by the application.
System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning': An attempt was made to lazy-load navigation 'CalendarRuleConditions' on a detached entity of type 'CalendarRuleProxy'. Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking'. This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
似乎“CalendarRule.CalendarRuleConditions”中包含的数据还没有加载。我不使用“AsNoTracking”,并且在我发布的 API 代码之前没有对 SaveChangesAsync() 的任何调用。我该如何解决这个问题?
这个问题类似于。
我认为问题出在 (CalendarRule)MemberwiseClone()
,您应该使用 Create
(ef) 或 CreateProxy
(efcore) intead new
.[=15= 创建实体]
这段代码没有任何意义:
foreach (CalendarRuleCondition condition in clonedRule.CalendarRuleConditions)
{
conditions.Add((CalendarRuleCondition)condition.Clone());
}
clonedRule.CalendarRuleConditions = conditions;
如果您从要克隆的实体实例中调用此 Clone() 方法,则应为:
foreach (CalendarRuleCondition condition in CalendarRuleConditions)
{
conditions.Add((CalendarRuleCondition)condition.Clone());
}
clonedRule.CalendarRuleConditions = conditions;
我们在此对象的延迟加载的 CalendarRuleConditions 中检查每个条件,为克隆构建克隆列表。即便如此,最好先加载 CalendarRuleConditions 并仅依赖延迟加载作为 fall-back.
我使用 Entity Framework 作为 SQL 服务器的 ORM。
这是我里面的代码 POST API:
var group = await _context.Groups.FindAsync(id);
foreach (CalendarRuleGroup calendarRuleGroup in group.CalendarRuleGroups)
{
CalendarRule calendarRule = _context.CalendarRules
.Where(cr => cr.CalendarRuleGroupId == calendarRuleGroup.Id)
.First();
CalendarRule calendarRuleTemp = (CalendarRule)calendarRule.Clone();
_context.CalendarRules.Add(calendarRuleTemp);
}
await _context.SaveChangesAsync();
这是 Clone() 函数:
public object Clone()
{
CalendarRule clonedRule = (CalendarRule)MemberwiseClone();
clonedRule.Calendar = null;
clonedRule.CalendarId = default;
clonedRule.CalendarRuleGroup = null;
clonedRule.CalendarRuleGroupId = default;
clonedRule.CalendarContexts = null;
clonedRule.Id = default;
List<CalendarRuleCondition> conditions = new List<CalendarRuleCondition>();
foreach (CalendarRuleCondition condition in clonedRule.CalendarRuleConditions)
{
conditions.Add((CalendarRuleCondition)condition.Clone());
}
clonedRule.CalendarRuleConditions = conditions;
return clonedRule;
}
这是 CalendarRule 的模型:
public class CalendarRule: ICloneable
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime StartingFrom { get; set; }
public DateTime? Until { get; set; }
public int? Count { get; set; }
public double Duration { get; set; }
public Frequency Frequency { get; set; }
public int Interval { get; set; }
public int Priority { get; set; }
public bool IsWorking { get; set; }
public int Efficiency { get; set; }
public int Cost { get; set; }
public bool AllowedSetup { get; set; }
public int Color { get; set; }
public string Description { get; set; }
public bool? IsActive { get; set; }
public int CalendarId { get; set; }
public int? CalendarRuleGroupId { get; set; }
public virtual Calendar Calendar { get; set; }
public virtual CalendarRuleGroup CalendarRuleGroup { get; set; }
public virtual ICollection<CalendarContext> CalendarContexts { get; set; }
public virtual ICollection<CalendarRuleCondition> CalendarRuleConditions { get; set; }
}
这是foreach里面的“clonedRule.CalendarRuleConditions”执行时出现的错误:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMG79T5K5ECK", Request id "0HMG79T5K5ECK:00000002": An unhandled exception was thrown by the application.
System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning': An attempt was made to lazy-load navigation 'CalendarRuleConditions' on a detached entity of type 'CalendarRuleProxy'. Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking'. This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
似乎“CalendarRule.CalendarRuleConditions”中包含的数据还没有加载。我不使用“AsNoTracking”,并且在我发布的 API 代码之前没有对 SaveChangesAsync() 的任何调用。我该如何解决这个问题?
这个问题类似于
我认为问题出在 (CalendarRule)MemberwiseClone()
,您应该使用 Create
(ef) 或 CreateProxy
(efcore) intead new
.[=15= 创建实体]
这段代码没有任何意义:
foreach (CalendarRuleCondition condition in clonedRule.CalendarRuleConditions)
{
conditions.Add((CalendarRuleCondition)condition.Clone());
}
clonedRule.CalendarRuleConditions = conditions;
如果您从要克隆的实体实例中调用此 Clone() 方法,则应为:
foreach (CalendarRuleCondition condition in CalendarRuleConditions)
{
conditions.Add((CalendarRuleCondition)condition.Clone());
}
clonedRule.CalendarRuleConditions = conditions;
我们在此对象的延迟加载的 CalendarRuleConditions 中检查每个条件,为克隆构建克隆列表。即便如此,最好先加载 CalendarRuleConditions 并仅依赖延迟加载作为 fall-back.