我有一套简单的3个表,EF知道这些表是相关的......
在我的存储库中,我执行以下内容......
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个表的所有适当的值......
那么,我在做什么/不这样做是阻止EF填充一个相关对象而不是另一个?
我知道我可以通过创建视图来解决这个问题,但我试图了解EF正在做什么。
修改 我在EF中使用Database First。这些是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; }
}
答案 0 :(得分:1)
您急切地加载所以您需要像这样更改您的代码。
<?php
//$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
//$today = date("m.d.y"); // 03.10.01
//$today = date("j, n, Y"); // 10, 3, 2001
//$today = date("Ymd"); // 20010310
//$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
//$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
//$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
//$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
//$today = date("H:i:s"); // 17:16:18
echo $today;
?>
OR
var result = localContext.LexiconTerms.Include(i=>i.Locale.Lexicon)
这是有用的文件