实体框架4.1 Ghost列

时间:2012-02-10 13:12:52

标签: c# sql entity-framework-4.1 entity-relationship

我目前在使用EF4.1时遇到了一些问题。 SQL的生成似乎与我期望基于我的类生成的内容不匹配。我有以下课程(这只是一个较小的一个较大的集合,但是,这是我似乎有问题的地方)......

public class CustomEntityContext : DbContext
{
    public CustomEntityContext()
             :base(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString) {}

    public DbSet<Person> People { get; set; }
    public DbSet<Occurrence> Occurrences { get; set; }
    public DbSet<OccurrenceSecurity> OccurrenceSecurities { get; set; }
}


[DataContract(IsReference = true)]
[Table("Occurrence")]
public class Occurrence 
{
    [DataMember] public int ID { get; set; }
    [DataMember] public string Number { get; set; }
    [DataMember] public bool? IsMOR { get; set; }
    [DataMember] public bool? IsConfidential { get; set; }
    [DataMember] public int? IncidentID { get; set; }
    [DataMember] public bool? CanPublish { get; set; }
    [DataMember] public bool? IsFeedbackRequired { get; set; }
    [DataMember] public bool? IsRegulatorReport { get; set; }
    [DataMember] public DateTime? RecordedDate { get; set; }
    [DataMember] public DateTime? ReportedDate { get; set; }
    [DataMember] public int? ReportTypeID { get; set; }
    [DataMember] public bool? IsMain { get; set; }
    [DataMember] public bool? IsRejected { get; set; }  
    [DataMember] public string Title { get; set; }
    [DataMember] public byte[] Version { get; set; }
    [DataMember] public string ReportDataXml { get; set; }
    [DataMember] public int? LocationID { get; set; }
    [DataMember, ForeignKey("RecordedByPersonID")] public Person Recorder { get; set; }
    [DataMember, ForeignKey("ReportedByPersonID")] public Person Reporter { get; set; }
}

[DataContract(IsReference = true)]
[Table("OccurrenceSecurity")]
public class OccurrenceSecurity
{
    [DataMember, Key, Column("PersonID", Order = 0)] public int PersonID { get; set; }
    [DataMember, ForeignKey("PersonID")] public Person Person { get; set; }
    [DataMember, Key, Column("OccurrenceID", Order = 1)] public int OccurrenceID { get; set;          
    [DataMember, ForeignKey("OccurrenceID")] public Occurrence Occurrence { get; set; }
}

[DataContract(IsReference = true)]
[Table("Person")]
public class Person 
{
    [DataMember] public int ID { get; set; }
    [DataMember] public string FullName { get; set; }
    //[DataMember] public Occurrence[] RecordedOccurrences { get; set; }
    //[DataMember] public Occurrence[] ReportedOccurrences { get; set; }
    //[DataMember] public OccurrenceSecurity[] OccurrenceSecurities { set; get; } 
}

当我通过Include方法询问OccurrenceSecurities时,我要求同时包括Occurrence和Person。随后,生成的SQL如下...

SELECT 
[Extent1].[PersonID] AS [PersonID], 
[Extent1].[OccurrenceID] AS [OccurrenceID], 
[Extent2].[ID] AS [ID], 
[Extent2].[FullName] AS [FullName], 
[Extent3].[ID] AS [ID1], 
[Extent3].[Number] AS [Number], 
[Extent3].[IsMOR] AS [IsMOR], 
[Extent3].[IsConfidential] AS [IsConfidential], 
[Extent3].[IncidentID] AS [IncidentID], 
[Extent3].[CanPublish] AS [CanPublish], 
[Extent3].[IsFeedbackRequired] AS [IsFeedbackRequired], 
[Extent3].[IsRegulatorReport] AS [IsRegulatorReport], 
[Extent3].[RecordedByPersonID] AS [RecordedByPersonID], 
[Extent3].[RecordedDate] AS [RecordedDate], 
[Extent3].[ReportedByPersonID] AS [ReportedByPersonID], 
[Extent3].[ReportedDate] AS [ReportedDate], 
[Extent3].[ReportTypeID] AS [ReportTypeID], 
[Extent3].[IsMain] AS [IsMain], 
[Extent3].[IsRejected] AS [IsRejected], 
[Extent3].[Title] AS [Title], 
[Extent3].[Version] AS [Version], 
[Extent3].[ReportDataXml] AS [ReportDataXml], 
[Extent3].[LocationID] AS [LocationID], 
[Extent3].[Person_ID] AS [Person_ID],               -- Where does this come from?
[Extent3].[Person_ID1] AS [Person_ID1]              -- Where does this come from?
FROM   [dbo].[OccurrenceSecurity] AS [Extent1]
INNER JOIN [dbo].[Person] AS [Extent2] ON [Extent1].[PersonID] = [Extent2].[ID]
LEFT OUTER JOIN [dbo].[Occurrence] AS [Extent3] ON [Extent1].[OccurrenceID] = [Extent3].[ID]

如您所见,Select块末尾有2列选择Person_ID和Person_ID1。这些不存在于我的基础表或我的对象中。

有谁知道它们来自哪里以及它们为何存在?

另外,我知道这是一个多对多关系,但是,OccurrenceSecurities表/类将扩展为容纳更多数据。

谢谢, 大卫

1 个答案:

答案 0 :(得分:2)

  

使用Include方法,我要求包括Occurrence和Person

EF将使用这些额外的列从查询结果中构造对象图。使用Include,您说“我只想执行一个存储命令,但我想检索许多对象”。而不是使用多个结果集(并非所有后备存储都支持),当您查询A.Include("B.C")时,EF会发出包含这样结果的查询:

columns for A1 columns for B1 columns for C1
columns for A1 columns for B1 columns for C2
columns for A1 columns for B1 columns for C3
columns for A1 columns for B2 columns for C4
columns for A1 columns for B2 columns for C5
columns for A2 columns for B3 columns for C6
columns for A2 columns for B3 columns for C7

然后将这些行拼接在一起,形成两个A,3 B和7 C s,并建立适当的关系。

您显示的特定额外列是由EF在拼接过程中使用的,是我的猜测。