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
    primarykey
    data
    text
    <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>
    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. COOne thought occurred to me in the fog of sleep. The Person class in this case is self-referencing. UpdatedBy and CreatedBy are both foreign key references to itself. When I get in the office I'll try running a similar sproc against a less complex class and see if the issue reoccurs.
      singulars
    2. COWell I finally found time today to create a definitive test. I created a standalone table with only two columns and created matching objects which derived from a base class that defined ID and mapped the ID to the table's ID column (Test_ID) in a configuration class. Same result - same error. Evidently the sproc call via SqlQuery ignores all that lovely Fluent-API configuration code and just looks for direct mappings. So what do I do now? *stumped*
      singulars
    3. COI'm wondering if implementing custom materializers may be the answer - or if perhaps we should pursue a different architecture path in this app, since we have so many pre-existing sprocs to deal with and so far making EF work with them doesn't seem to be an easy task.
      singulars
 

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