Object cannot be cast from DBNull to other types Entity Framework
I've got a column in one of my models that is nullable. It is nullable in the EDMX, and I've checked that it's nullable in the generated code. I've octuple-checked that it's nullable in the database. However, when I try to save an instance of the model with the column set to null, I receive the exception "Object cannot be cast from DBNull to other types." Most of the code involved here is either the Entity Framework code itself, or generated code. I have other nullable columns that do not seem to have this problem.
Has anyone run into anything like this? 开发者_开发问答Googling around for things mostly reveals people who need to do if (someSqlValue == DBNull.Value)
with manual ADO recordsets, but since this is EF interacting with DBNull, there's nowhere in my code that could possibly need to check this.
Unfortunately, I cannot share the code with this, and as I mentioned, most of my nullable columns do not exhibit this problem, so I am not confident in my ability to reproduce the problem in a small test case.
Have you checked that the type in your EDMX matches the type in your database (i.e. that you aren't trying to stuff a null int SQL value into a nullable DateTime field or something like that)?
精彩评论