我有一个 edmx 数据存储,我正在尝试针对它执行存储过程:
.
.
.
CustomerDb.ExecuteStoreQuery<Customer>("GetCustomers", parameters).ToList();
客户类别具有以下结构
class Customer {
public int Id { get; set; }
.
.
.
public Address Address { get; set; }
}
class Address {
public int Id { get; set; }
.
.
.
public string PostCode { get; set; }
}
现在,无论我做什么,Address
属性始终为 null。我尝试过以不同的格式返回结果集,但无论我做什么它总是为空。
例如
SELECT c.Id, ..., a.PostCode
FROM Customer AS c
INNER JOIN Address AS a ON c.CustomerId = a.CustomerId
WHERE c.CustomerId = @CustomerId
或
SELECT c.Id, ..., a.PostCode AS 'Address.PostCode'
FROM Customer AS c
INNER JOIN Address AS a ON c.CustomerId = a.CustomerId
WHERE c.CustomerId = @CustomerId
或
SELECT c.Id, ..., a.PostCode AS 'Address_PostCode'
FROM Customer AS c
INNER JOIN Address AS a ON c.CustomerId = a.CustomerId
WHERE c.CustomerId = @CustomerId
但是这些列永远不会被拾取。
我做错了什么?
谢谢。
最佳答案
我认为你不能这样做。根据MSDN
Each property of the type:
• Must have a setter.
• Must correspond to a primitive type in CSDL.
• Must correspond to a column name in the resulting DbDataReader (the provider implementation determines whether a column has the same name as the property). If the name of the type's property does not match a field of the DbDataReader, the Entity Framework materializes the default value of the property if it is defined in the conceptutal model.
您的地址是 CSDL 中的 ComplexType。我认为您必须使用可以通过 ExecuteStoreQuery 实现的类型来构造对象。
关于c# - Entity Framework 存储过程 - 映射复杂属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16481771/