实体框架4.1幽灵列

本文关键字:幽灵 框架 实体 | 更新日期: 2023-09-27 18:23:57

我目前在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块的末尾有两列,分别选择Person_ID和Person_ID1。这些不存在于我的底层表或对象中。

有人知道这些东西从哪里来,为什么会在那里吗?

此外,我知道这是一种多对多的关系,然而,OccurrenceSecurities表/类将扩展以容纳更多数据。

谢谢,David

实体框架4.1幽灵列

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

EF将使用这些额外的列来根据查询结果构建对象图。使用Include,表示"我只想执行一个存储命令,但我想检索多个

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个Bs和7个Cs。

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