DbContext.Database.SqlQuery< TEntity>(sqlcmd,params)不处理ComplexType映射 - 这是一个错误还是“按设计”? [英] DbContext.Database.SqlQuery<TEntity>(sqlcmd, params) does not handle ComplexType mapping - Is this a bug or "By Design"?
问题描述
如果我在我的Entity中包含ComplexType,则此方法返回以下错误消息:"InvalidOperationException未处理Cannog为类型为'CodeFirstNamespace.WellCompany'的属性'owner'创建值。仅支持具有原始类型
的属性。"
If I include a ComplexType in my Entity this method returns the following error message: "InvalidOperationException was unhandled Cannog create a value for property 'owner' of type 'CodeFirstNamespace.WellCompany'. Only properties with primitive types are supported."
源代码列表:
公共部分班级好$
{
public Well()
{
this.owner = new WellCompany();
}
//原始属性
public string wellNumber {get;组; }
public string wellName {get;组; }
public string wellType {get;组; }
&NBSP;&NBSP;&NBSP;
//复杂属性
公共WellCompany所有者{get;组; }
}
public partial class Well
{
public Well()
{
this.owner = new WellCompany();
}
// Primitive properties
public string wellNumber { get; set; }
public string wellName { get; set; }
public string wellType { get; set; }
// Complex properties
public WellCompany owner { get; set; }
}
公共部分类WellCompany
{
public WellCompany()
//原始属性
公共双重公司{get;组; }
public string companyName {get;组; }
public string companyPhone {get;组; }
}
public partial class WellCompany
{
public WellCompany()
// Primitive properties
public double companyId { get; set; }
public string companyName { get; set; }
public string companyPhone { get; set; }
}
公共部分类WellContext:DbContext
{
public WellContext()
:base(" name = WellContext")
{
}
//公共实体
public DbSet< Well>威尔斯{得到;组; }
}
public partial class WellContext : DbContext
{
public WellContext()
: base("name=WellContext")
{
}
// Public Entities
public DbSet<Well> Wells { get; set; }
}
课程计划
{
static void Main(string [] args)
{
使用(WellContext db = new WellContext())
{
string sqlcmd =" SELECT Wells.wellNumber,Wells.wellName,Wells.wellType,WellCompanies.companyId,WellCompanies.companyName, " +
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; " WellCompanies.companyPhone
从井LEFT OUTER JOIN WellCompanies ON Wells.OpNo = WellCompanies.companyId英寸;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; IEnumerable的<那么>的SQLQuery = db.Database.SqlQuery<那么>(SQLCMD);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP; foreach(sqlQuery中的项目)
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Console.WriteLine(" - {0} - {1}",item.wellNumber,item.owner.companyName);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
Console.WriteLine("按任意键继续。");
$
&NBSP;&NBSP; Console.ReadKey();
$
}
}
}
class Program
{
static void Main(string[] args)
{
using (WellContext db = new WellContext())
{
string sqlcmd = "SELECT Wells.wellNumber, Wells.wellName, Wells.wellType, WellCompanies.companyId, WellCompanies.companyName, " +
"WellCompanies.companyPhone
FROM Wells LEFT OUTER JOIN WellCompanies ON Wells.OpNo = WellCompanies.companyId";
IEnumerable<Well> sqlQuery = db.Database.SqlQuery<Well>(sqlcmd);
foreach (Well item in sqlQuery)
{
Console.WriteLine(" - {0} - {1}", item.wellNumber, item.owner.companyName);
}
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
}
推荐答案
SqlQuery不会通过常规EF查询所执行的映射引擎。因此,如果你有一个复杂的类型或继承结构,它将遇到异常。 SqlQuery仅支持基本名称匹配以填充实体。
SqlQuery does not go through the mapping engine which a regular EF query does. hence, if u have a complex type or inheritance structure, it will run into exception. SqlQuery only supports basic name matching to populate the entity.
现在这是我的全部猜测。 我在ExecuteStoreQuery上发布了近两年的类似问题,我收到了Diego的回复。你可以在这里阅读。
Now this is all my guess. i posted a similar question close to two years back on ExecuteStoreQuery and i got a response from Diego. You can read it here.
http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/44cf5582-63f8-4f81-8029-7b43469c028d
这篇关于DbContext.Database.SqlQuery< TEntity>(sqlcmd,params)不处理ComplexType映射 - 这是一个错误还是“按设计”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!