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

时间:2021-07-05 16:27:50

We are using DB First Entity Framework in which We have a stored procedure which would return all the columns but one for a table. When I try to map that stored procedure to return the entity type it crashes saying

我们正在使用DB First Entity Framework,其中我们有一个存储过程,它将返回所有列,但只返回一个表。当我尝试映射该存储过程以返回实体类型时,它崩溃说

"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."

I know I can create a complex type to match what sp is returning, or I can return a default value from sp itself, but what I here want to know is, is there anyway by which I can ask ef to set '0' into MyTableId if it was not returned by stored procedure ?

我知道我可以创建一个复杂的类型来匹配sp返回的内容,或者我可以从sp本身返回一个默认值,但我想知道的是,无论如何,我可以请求ef将'0'设置为如果存储过程没有返回MyTableId?

1 个解决方案

#1


1  

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

Quote from Article:

文章引用:

Note:EF does not take any mapping into account when it creates entities using the Translate method. It will simply match column names in the result set with property names on your classes.

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

You must either mark the property as [NotMapped], or return the Column with a default value via the stored procedure.

您必须将属性标记为[NotMapped],或通过存储过程返回具有默认值的列。

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

Make sure you have the correct datatype in the convert statement.

确保在convert语句中具有正确的数据类型。

#1


1  

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

Quote from Article:

文章引用:

Note:EF does not take any mapping into account when it creates entities using the Translate method. It will simply match column names in the result set with property names on your classes.

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

You must either mark the property as [NotMapped], or return the Column with a default value via the stored procedure.

您必须将属性标记为[NotMapped],或通过存储过程返回具有默认值的列。

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

Make sure you have the correct datatype in the convert statement.

确保在convert语句中具有正确的数据类型。