实体框架:将null设置为false,但类型仍为空 [英] Entity Framework: Set nullable to false, but type is still nullable
问题描述
这个问题与在Visual Studio 2012中创建的一个新的类库项目有关。
我已经向我的实体框架模型添加了一个SQL Server视图。
在 PromisedDate
列的属性中,我将Nullable从(无)
到 False
,因为这个列中不会返回一个空值。我有双重检查,没有null值与select语句。
但是,即使在保存之后,关闭所有打开的Visual Studio中的选项卡,并进行全面清理和重建的解决方案, PromisedDate
仍然是一个 DateTime?
,我必须继续使用 .Value
令人沮丧的是,我做错了什么?
以下是生成的.CS文件的屏幕快照:
您应该执行以下步骤。 >
1)保存设计器文件并从设计器生成数据库
2)单击上下文文件和代码生成文件(T4),然后单击运行自定义工具。 br>
3)确保您再次保存文件,生成数据库后将显示*。
This question relates to a new class library project created in Visual Studio 2012.
I have added a SQL Server view to my entity framework model.
In the properties of the PromisedDate
column, I have changed Nullable from (None)
to False
as there will never be a null value returned in this column. I have double-checked there are no null values with a select statement.
However, even after a save, closing all open tabs in Visual Studio and doing a full clean and rebuild of the solution, PromisedDate
is still a DateTime?
and I have to keep using .Value
in code to get the value of it.
It's frustrating, what am I doing wrong?!
Here is a screenshot of the generated .CS file:
You should do the following steps.
1) Save designer file and generate your database from designer
2) Click on Context file and Code Generation file(T4) and click run custom tool.
3) make sure you save the file again, an * will appear after you generate the database.
这篇关于实体框架:将null设置为false,但类型仍为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!