与标题一样,当我将子项添加到现有父项时,将检索所有现有子项。
缩小/简化的父母和子女模特:
public class ProjectLocalisation
{
public int ProjectID { get; set; }
public virtual Project Project { get; set; }
public virtual ICollection<ProjectImage> Images { get; set; }
}
public class ProjectImage
{
[Key]
public int ID { get; set; }
public int ProjectID { get; set; }
public virtual ProjectLocalisation ProjectLocalisation { get; set; }
}
通过创建类的新实例,设置主键和附加对象来完成编辑现有父级。这一行执行的那一刻:
projectLocalisation.Images.Add(image);
执行此查询:
exec sp_executesql N'SELECT
[Extent1].[ID] AS [ID],
[Extent1].[ProjectID] AS [ProjectID],
[Extent1].[Name] AS [Name],
[Extent1].[Order] AS [Order],
[Extent1].[LanguageTag] AS [LanguageTag]
FROM [dbo].[ProjectImages] AS [Extent1]
WHERE ([Extent1].[ProjectID] = @EntityKeyValue1) AND ([Extent1].[LanguageTag] = @EntityKeyValue2)',N'@EntityKeyValue1 int,@EntityKeyValue2 nvarchar(128)',@EntityKeyValue1=5,@EntityKeyValue2=N'nl'
有没有办法禁用此行为?
编辑:一个解决方法似乎是不让集合虚拟化,但我宁愿不要在整个地方使用.Include()
。
答案 0 :(得分:2)
这看起来像延迟加载。当你不需要它时(或当你不需要它时)通过调用:
来关闭延迟加载dbContext.Configuration.LazyLoadingEnabled = false;
答案 1 :(得分:0)
您可以尝试从关系的另一端添加图片,例如
db.ProjectImages.Add(new ProjectImage { ProjectLocalisation = localisation, ... }