简体   繁体   中英

How do I provide a default value to EF Model when Stored Procedure does not return specified column

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

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM