gpt4 book ai didi

c# - Entity Framework 不会包含()所有表

转载 作者:太空狗 更新时间:2023-10-29 20:38:52 25 4
gpt4 key购买 nike

我有一组简单的 3 个表,EF 知道它们是相关的...

Lexicon<=>LexiconTerms

Local<=>LexiconTerms

在我的存储库中,我执行以下...

var result = localContext.LexiconTerms.Include(i=>i.Locale).Include(i=>i.Lexicon)

当我检查结果时 Lexicon 总是被填充并且 Locale 总是 null

现在,如果我查看生成的 SQL,我会看到这个......

SELECT 
[Extent1].[Id] AS [Id],
[Extent1].[LexiconId] AS [LexiconId],
[Extent1].[ResourceId] AS [ResourceId],
[Extent1].[Value] AS [Value],
[Extent1].[LocaleId] AS [LocaleId],
[Extent1].[ResourceSet] AS [ResourceSet],
[Extent1].[Type] AS [Type],
[Extent1].[BinFile] AS [BinFile],
[Extent1].[TextFile] AS [TextFile],
[Extent1].[Filename] AS [Filename],
[Extent1].[Comment] AS [Comment],
[Extent1].[ValueType] AS [ValueType],
[Extent1].[Updated] AS [Updated],
[Extent2].[Name] AS [Name],
[Extent2].[LocaleCode] AS [LocaleCode],
[Extent3].[Id] AS [Id1],
[Extent3].[Name] AS [Name1],
[Extent3].[Description] AS [Description],
[Extent3].[DeletedOn] AS [DeletedOn]
FROM [Locale].[LexiconTerms] AS [Extent1]
INNER JOIN [Locale].[Locale] AS [Extent2] ON [Extent1].[LocaleId] = [Extent2].[LocaleCode]
INNER JOIN [Locale].[Lexicon] AS [Extent3] ON [Extent1].[LexiconId] = [Extent3].[Id]
WHERE (N'Test1' = [Extent1].[ResourceId]) AND (N'UnitTest' = [Extent1].[ResourceSet]) AND (-2 = [Extent1].[LexiconId]) AND ([Extent1].[LocaleId] = @p__linq__0)

这清楚地表明 EF 正在获取数据。此外,在 SSMS 中运行 SQL,我可以看到所有 3 个表的所有适当值...

SQL Results

那么,我在做什么/不做什么是为了阻止 EF 填充一个相关对象而不是另一个?

我知道我可以通过创建 View 等方式解决这个问题,但我试图了解 EF 正在做什么。

编辑:我在 EF 中使用数据库优先。这些是 EF 生成的类...

public partial class Lexicon
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Lexicon()
{
this.LexiconTerms = new HashSet<LexiconTerm>();
}

public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<System.DateTime> DeletedOn { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LexiconTerm> LexiconTerms { get; set; }
}

public partial class LexiconTerm
{
public int Id { get; set; }
public int LexiconId { get; set; }
public string ResourceId { get; set; }
public string Value { get; set; }
public string LocaleId { get; set; }
public string ResourceSet { get; set; }
public string Type { get; set; }
public byte[] BinFile { get; set; }
public string TextFile { get; set; }
public string Filename { get; set; }
public string Comment { get; set; }
public int ValueType { get; set; }
public Nullable<System.DateTime> Updated { get; set; }

public virtual Lexicon Lexicon { get; set; }
public virtual Locale Locale { get; set; }
}

public partial class Locale
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Locale()
{
this.LexiconTerms = new HashSet<LexiconTerm>();
}

public string Name { get; set; }
public string LocaleCode { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<LexiconTerm> LexiconTerms { get; set; }
}

最佳答案

您使用急切加载,所以您需要像这样更改您的代码。

var result = localContext.LexiconTerms.Include(i=>i.Locale.Lexicon) 

var result = localContext.LexiconTerms.Include("Locale.Lexicon") 

有用的文档

https://msdn.microsoft.com/en-us/data/jj574232.aspx

关于c# - Entity Framework 不会包含()所有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com