c# - Entity Framework 存储过程 - 映射复杂属性

标签 c# .net entity-framework

我有一个 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/

相关文章:

c# - C# VS2010 项目中的 "PDB does not match image"错误

c# - 一或零到多,代码优先

C# - 如何在 Entity Framework Core 3.1 的模型中动态设置 builder.Property().HasComment

c# - 使用 yield 和 foreach 迭代自定义对象集合,无需装箱/拆箱

c# - 非锁定 TextWriterTraceListener?

c# - 发生数据库错误后 DbContext 是否可以安全使用

c# - LINQ to SQL 中的堆栈溢出异常

c# - 将 DocumentDB 与表单例份验证和角色结合使用

c# - WPF Listview-基于按钮单击显示来自不同 ObservableCollections 的数据的最佳方式是什么?

C# .net Web 服务 Web 方法在查看源代码中返回 < 和 >