gpt4 book ai didi

c# - Entity Framework 4.1 幽灵列

转载 作者:太空狗 更新时间:2023-10-29 23:40:57 25 4
gpt4 key购买 nike

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

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]

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

有谁知道这些是从哪里来的,为什么会在那里?

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

谢谢,大卫

最佳答案

with the Include methods, I ask to include both the Occurrence and 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,并具有适当的关系。

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

关于c# - Entity Framework 4.1 幽灵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9228333/

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