当存储过程未返回指定的列时,如何为EF模型提供默认值



我们使用的是DB第一个实体框架,其中我们有一个存储过程,该过程将返回所有列,但一个用于表。当我尝试映射该存储的过程以返回崩溃的实体类型

"The data reader is incompatible with the specified 'ReservingModel.t_MyTable'. A member of the type, 'MyTableID', does not have a corresponding column in the data reader with the same name."

我知道我可以创建一个复杂的类型来匹配SP的返回,或者我可以从SP本身返回默认值,但是我想知道的是,无论如何我可以要求EF设置EF'如果未通过存储过程返回MyTableID?

http://msdn.microsoft.com/en-us/data/jj691402

文章引用:

注意:EF使用翻译方法创建实体时不会考虑任何映射。它将仅将结果集中的列名与类上的属性名称匹配。

您必须将属性标记为[未映射],或者通过存储过程返回列表。

Select
ColA, ColB, ColC ,
[MyColumnName] = convert(bit , 0) /* this would mimic a false (dotnet) , aka, bit in sql-server */
from
dbo.MyTable

确保您在转换语句中具有正确的数据类型。