简体   繁体   English

实体框架:将nullable设置为false,但是type仍然可以为null

[英]Entity Framework: Set nullable to false, but type is still nullable

This question relates to a new class library project created in Visual Studio 2012. 此问题与在Visual Studio 2012中创建的新类库项目有关。

I have added a SQL Server view to my entity framework model. 我已经在实体框架模型中添加了SQL Server视图。

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. PromisedDate列的属性中,我将Nullable从(None)更改为False因为在此列中永远不会返回空值。 I have double-checked there are no null values with a select statement. 我已经使用select语句仔细检查了没有空值。

edmx的屏幕截图,显示nullable设置为false

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? 但是,即使保存后关闭了Visual Studio中所有打开的选项卡并进行了完整的清理和重新生成解决方案, PromisedDate仍然是DateTime? and I have to keep using .Value in code to get the value of it. 而且我必须继续在代码中使用.Value来获取它的值。

It's frustrating, what am I doing wrong?! 令人沮丧的是,我在做什么错?!

Here is a screenshot of the generated .CS file: 这是生成的.CS文件的屏幕截图:

生成的类的屏幕截图显示仍可为空

You should do the following steps. 您应该执行以下步骤。

1) Save designer file and generate your database from designer 1)保存设计器文件并从设计器生成数据库
2) Click on Context file and Code Generation file(T4) and click run custom tool. 2)单击上下文文件和代码生成文件(T4),然后单击运行自定义工具。
3) make sure you save the file again, an * will appear after you generate the database. 3)确保再次保存文件,生成数据库后会出现一个*。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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