我正在使用 Entity Framework 5(使用 Code First 方法)从带有参数的遗留存储过程填充我的一个类,这工作正常(详细信息如下)。
我的问题是我想将列的名称映射到具有不同名称的属性(我不喜欢来自 Erp 的名称)。
我尝试使用 Configuration 类(就像我映射到 View 或表时所做的那样)为具有不同名称的属性指定列名,以下是我的结果:
为什么配置会影响我的结果,但它的规范不用于映射列?是否有另一种方法可以使用 SqlQuery 指定此映射?
以下是详细信息:
我的 POCO 类(class):
public class Item
{
public String Id { get; set; }
public String Description { get; set; }
}
配置类:
public class ItemConfiguration : EntityTypeConfiguration<Item>
{
public ItemConfiguration()
{
HasKey(x => new { x.Id });
Property(x => x.Id).HasColumnName("Code");
Property(x => x.Description).HasColumnName("ItemDescription");
}
}
存储过程返回带有“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);
有了这个,我将配置添加到上下文中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ItemConfiguration());
}
谢谢!
最佳答案
我在这里找到:
http://entityframework.codeplex.com/workitem/233?PendingVoteId=233
“SqlQuery 方法旨在不考虑任何映射......”。
他们还说“我们同意让 SqlQuery 尊重列属性的选项会很有用,所以我们保持这个问题开放并将其放在我们的待办事项中以备将来考虑。”所以,如果你有我同样的问题,请投票:-)
关于stored-procedures - Entity Framework Code First - 为 SqlQuery 配置映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14733047/