我有一个 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 抛出异常无法找到列