Created
March 13, 2013 07:14
-
-
Save yicone/5149927 to your computer and use it in GitHub Desktop.
@vwxyzh EF里遇到一个诡异的问题:某表增加了一个字段A(类型为short?),更新了Entity, 程序中第一次使用该Entity的地方,是IQueryable<entity>.Min(r => r.B),当程序连接到另一台未添加字段的数据库,运行时并没有认为表结构有差异,而是返回这么一个异常:
Min "The cast to value type 'Int16' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修改前报异常。Entity中缺少新增的字段A,但是返回的异常是一个类型转换相关的 | |
short minSecId = queryNoCached.Min(prc => prc.CacheHour); | |
// 修改后, 运行时容忍了Entity中缺少了新增的字段 | |
short minSecId = queryNoCached.Min(prc => (short?)prc.CacheHour) ?? 0; |
we do have a universal solution to these kind of problems:
step 1. restart your ide, if no effect go to step 2
step 2. restart your pc, if no effect go to step 3
step 3. try carefully re-install your ide, if no effect go to step 4
step 4. try re-install your os, if no effect go to step 5
step 5. redo from step 1 more carefully.
LSS的回答太搞笑了。。。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
呃。。。