Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework code first foreign key select query issue
    primarykey
    data
    text
    <p>I have a POCO which has a foreign key that looks like this</p> <pre><code>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&lt;DateTime&gt; StartDate { get; set; } public Nullable&lt;DateTime&gt; EndDate { get; set; } public Nullable&lt;DateTime&gt; UpdatedDateTime { get; set; } } </code></pre> <p>Mappings:</p> <pre><code>class DataFileUploadInformationEntityMap : EntityTypeConfiguration&lt;DataFileInformation&gt; { public DataFileUploadInformationEntityMap () { ToTable("DataFileInformationsView"); HasKey(fileMetaData =&gt; fileMetaData.ID); HasOptional(file =&gt; file.History) .WithMany().HasForeignKey(file =&gt; file.HistoryID); } } public class JobHistoryEntityMap : EntityTypeConfiguration&lt;JobHistory&gt; { public JobHistoryEntityMap() { ToTable("JobHistoryView"); HasKey(job =&gt; job.ID); } } </code></pre> <p>Even with the configuration the dynamic query getting fired looks like this</p> <pre><code>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 </code></pre> <p>The dynamic query getting generated is unable to map the JobHistory_ID and the DataFileInformation_ID to the POCO and EF throws an exception unable to find columns</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload