Note that there are some explanatory texts on larger screens.

plurals
  1. POEF4.1 - Fluent API - SqlQuery - configuration mappings when calling sproc - Data reader is incompatible with specified entity type
    text
    copied!<p>The scenario - legacy application with 10 year history, has always used procedure calls for all data access - needs to be overhauled from a hybrid classic ASP and .NET set of pages.</p> <p>The goal - migrate to .NET 4.0 using EF 4.1 with Fluent API and continue using existing database sprocs as much as possible.</p> <p>Key classes:</p> <pre><code>public class EntityBase { public int Id { get; set; } } public class User : EntityBase { public string UserName { get; set; } ... } </code></pre> <p>Configurations:</p> <pre><code>internal class ConfigurationBase&lt;T&gt; : EntityTypeConfiguration&lt;T&gt; where T : EntityBase { protected ConfigurationBase() { HasKey(t =&gt; t.Id); } } internal class UserConfiguration : ConfigurationBase&lt;User&gt; { internal UserConfiguration() { Property(p =&gt; p.Id) .HasColumnName("Person_Id") .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) .IsRequired(); Property(p =&gt; p.UserName) .HasMaxLength(64); ToTable("Person"); } } </code></pre> <p>The context is all set up in DbContext.OnModelCreating as:</p> <pre><code> public DbSet&lt;User&gt; Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new UserConfiguration()); } </code></pre> <p>And all is fine when I access the data directly via the context eg:</p> <pre><code> public override IQueryable&lt;User&gt; GetAll() { return UnitOfWork.Context.Users; } </code></pre> <p>But when I attempt to use an existing sproc which contains the following:</p> <pre><code>SELECT p.Person_Id, p.IsUser, p.FirstName, p.LastName, p.UserName, p.Email, p.CreatedBy, p.CreatedDate, p.IsActive, p.ModifiedBy, p.ModifiedDate FROM Person p WHERE p.UserName = @UserName AND p.IsActive = 1 </code></pre> <p>I execute the following:</p> <pre><code> public User AuthorizeUser(string userName) { SqlParameter p = new SqlParameter { DbType = DbType.String, ParameterName = "UserName", Size = 64, Value = userName }; object[] parameters = new object[] {p}; return UnitOfWork.Context.Users.SqlQuery(CPODSStoredProcedures.User_AuthorizeUser, parameters).FirstOrDefault(); } </code></pre> <p>And I get: The data reader is incompatible with the specified 'User'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.</p> <p>I have traced the execution and the configurations are being read, so am I doing something wrong, or is the sproc execution by SqlQuery not paying attention to how the base Id is remapped to Person_Id in this case.</p> <p>Thanks in advance! G</p>
 

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