Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to map results of a stored procedure to an existing table in Entity Framework code-first
    primarykey
    data
    text
    <p>I have an issue while mapping results of a stored procedure (<code>dbo.sp_Get_User</code>) to an existing <code>UserEntity</code> entity.</p> <p>I didn't want to user db table column names in my project directly. So I have <code>UserEntity</code> as follows:</p> <pre><code>public class UserEntity { public UserEntity() { } public int UserKey { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } </code></pre> <p>And my mapper maps all the columns of table to this entity as follows:</p> <pre><code>public class UserEntityMapper : EntityTypeConfiguration&lt;UserEntity&gt; { public UserEntityMapper() { ToTable("tbl_User"); HasKey(m =&gt; m.UserKey); Property(p =&gt; p.UserKey).HasColumnName("User_KEY"); Property(p =&gt; p.FirstName).HasColumnName("FIRST_NAME"); Property(p =&gt; p.LastName).HasColumnName("LAST_NAME"); } } </code></pre> <p>Now I have a condition where I need to use a stored procedure to get the user data (based on some dynamic queries and all, I have to use this stored procedure for sure)</p> <p>So I am trying to execute the stored procedure as follows:</p> <pre><code>SqlParameter temp1 = new SqlParameter("@EffecitveDate", DateTime.Parse("01/01/2012")); SqlParameter temp2 = new SqlParameter("@User_Key", id); IEnumerable&lt;UserEntity&gt; usrEnt = context.Database.SqlQuery&lt;UserEntity&gt;("sp_Get_User @EffecitveDate, @User_Key", temp1, temp2); </code></pre> <p>But I am getting following error while executing the above line:</p> <blockquote> <p>The data reader is incompatible with the specified 'UserEntity'. A member of the type, 'UserKey', does not have a corresponding column in the data reader with the same name.</p> </blockquote> <p>I am really stuck on this please suggest. Thanks</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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