实体框架Code First - 为SqlQuery配置映射

Dan*_*sco 13 mapping configuration stored-procedures ef-code-first entity-framework-5

我正在使用Entity Framework 5(使用Code First方法)从带有参数的遗留存储过程填充我的一类,这很正常(详情如下).我的问题是我想将列的名称映射到具有不同名称的属性(我不喜欢来自Erp的名称).我尝试使用Configuration类(就像我映射到视图或表时所做的那样)来为具有不同名称的属性指定列名,这是我的结果:

  • 如果我不使用配置类(我不在DbContext的OnModelCreating方法中添加它),那么EF工作但只加载与列名称完全匹配的属性(这就是我在此期望的案件); 其他财产是空的;
  • 如果我使用配置类(在DbContext的OnModelCreating方法中将其添加到modelBuilder),则EF会引发一个异常,指出"数据读取器与指定的'... Item'不兼容.该类型的成员,'说明',数据读取器中没有相应的列具有相同的名称",这对我来说听起来很奇怪,因为在配置中我指定属性Description映射到列ItemDescription.

为什么配置会影响我的结果,但其规范不用于映射列?有没有其他方法使用SqlQuery指定此映射?

以下是详细信息:

我的POCO课程:

public class Item
    {
        public String Id { get; set; }
        public String Description { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

配置类:

public class ItemConfiguration : EntityTypeConfiguration<Item>
    {
        public ItemConfiguration()
        {
            HasKey(x => new { x.Id });
            Property(x => x.Id).HasColumnName("Code");
            Property(x => x.Description).HasColumnName("ItemDescription");
        }
    }
Run Code Online (Sandbox Code Playgroud)

存储过程返回带有"Code"和"ItemDescription"列的数据; 我用这种方式称呼它:

var par = new SqlParameter();
par.ParameterName = "@my_par";
par.Direction = ParameterDirection.Input;
par.SqlDbType = SqlDbType.VarChar;
par.Size = 20;
par.Value = ...;

var data = _context.Database.SqlQuery<Item>("exec spItem @my_par", par);
Run Code Online (Sandbox Code Playgroud)

并且我将配置添加到上下文中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
      modelBuilder.Configurations.Add(new ItemConfiguration());
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Dan*_*sco 11

我在这里找到:

http://entityframework.codeplex.com/workitem/233?PendingVoteId=233

"SqlQuery方法的设计不考虑任何映射......".

他们还说"我们同意选择使SqlQuery荣誉列属性是有用的,所以我们将这个问题保持打开状态并将其放在我们的待办事项上以供将来考虑.",所以,如果你有同样的问题,请投票:-)

  • 好吧,它是2013年,似乎仍然无法运作. (2认同)
  • 2015年3月.它现在提出了地位.在codeplex上投了票. (2认同)