このような外部キーを持つPOCOがあります
public partial class DataFileInformation
{
public DataFileInformation()
{
}
public int ID { get; set; }
public int? HistoryID { get; set; }
public string FileName { get; set; }
public int NumberOfLines { get; set; }
public string UpdatedByPersonnelNumber { get; set; }
public DateTime UpdatedDateTime { get; set; }
public virtual JobHistory History { get; set; }
}
public partial class JobHistory
{
public int ID { get; set; }
public Nullable<DateTime> StartDate { get; set; }
public Nullable<DateTime> EndDate { get; set; }
public Nullable<DateTime> UpdatedDateTime { get; set; }
}
マッピング:
class DataFileUploadInformationEntityMap : EntityTypeConfiguration<DataFileInformation>
{
public DataFileUploadInformationEntityMap ()
{
ToTable("DataFileInformationsView");
HasKey(fileMetaData => fileMetaData.ID);
HasOptional(file => file.History)
.WithMany().HasForeignKey(file => file.HistoryID);
}
}
public class JobHistoryEntityMap : EntityTypeConfiguration<JobHistory>
{
public JobHistoryEntityMap()
{
ToTable("JobHistoryView");
HasKey(job => job.ID);
}
}
構成を使用しても、実行される動的クエリは次のようになります。
exec sp_executesql N'SELECT
[Extent1].[ID] AS [ID],
[Extent1].[DataFileInformation_ID] AS [DataFileInformation_ID],
[Extent1].[FileName] AS [FileName],
[Extent1].[UpdatedByPersonnelNumber] AS [UpdatedByPersonnelNumber],
[Extent1].[UpdatedDateTime] AS [UpdatedDateTime],
[Extent1].[JobID] AS [LoaderJobID],
[Extent1].[JobHistory_ID] AS [JobHistory_ID],
[Extent2].[ID] AS [ID1],
[Extent2].[StartDate] AS [StartDate],
[Extent2].[EndDate] AS [EndDate],
[Extent2].[UpdatedByPersonnelNumber] AS [UpdatedByPersonnelNumber1],
[Extent2].[CreateDate] AS [CreateDate],
FROM [DataFileInformationsView] AS [Extent1]
LEFT OUTER JOIN [JobHistoryView] AS [Extent2]
ON [Extent1].[LoaderJobID] = [Extent2].[ID]
WHERE [Extent1].[ID] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=146
生成される動的クエリは、JobHistory_IDとDataFileInformation_IDをPOCOにマップできず、EFは列を見つけることができない例外をスローします