[英]Entity Framework making double a nullable property
I have a property called TotalVolume
which is a Double
. 我有一个名为TotalVolume
的属性,它是一个Double
。 In the code-first approach when the database is constructed the property TotalVolume
has got a Not Null
value by default. 在构建数据库的代码优先方法中,默认情况下,属性TotalVolume
具有一个Not Null
值。 I want to make the property TotalVolume
accept Null
values too. 我也想让属性TotalVolume
接受Null
值。
I am using FluentAPI, and the change I made is as follows: 我正在使用FluentAPI,所做的更改如下:
modelBuilder.Entity<Employee>()
.Property(p => p.TotalVolume)
.IsRequired(false);
I end up with the following error when I Add-Migration
: 我在Add-Migration
时遇到以下错误:
The property 'TotalVolume' on entity type 'Employee' cannot be marked as nullable/optional because the type of the property is 'double' which is not a nullable type. 不能将实体类型“雇员”上的属性“ TotalVolume”标记为可为空/可选,因为属性的类型为“ double”,这不是可为空的类型。 Any property can be marked as non-nullable/required, but only properties of nullable types and which are not part of primary key can be marked as nullable/optional. 任何属性都可以标记为不可为空/必需,但是只有可为空类型且不属于主键的属性可以被标记为可为空/可选。
Thereafter, I tried the following approach, where I made the following change in my Employee
Model. 此后,我尝试了以下方法,在“ Employee
模型”中进行了以下更改。
public double? TotalVolume{ get; set; }
However, when I updated the database, the datatype for TotalVolume
has changed to a float
. 但是,当我更新数据库时, TotalVolume
的数据类型已更改为float
。 I want the datatype to still remain as double
. 我希望数据类型仍然保持为double
。
float
in SQL is a 64bit value and corresponds to double
in .Net, so it seems to be working as expected. SQL中的float
是一个64位值,与.Net中的double
相对应,因此它似乎按预期工作。
Entity Framework maps these types so that they have the same meaning but it doesn't require they have the same names. 实体框架会映射这些类型,以便它们具有相同的含义,但不需要它们具有相同的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.