简体   繁体   English

实体框架使双精度为可空属性

[英]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.

相关问题 实体框架不可为空的列映射到可为空的实体属性 - Entity Framework Non-nullable column is mapped to a nullable entity property 实体框架中不可为空的导航属性为null - Non nullable navigation property is null in Entity Framework 在实体框架中以编程方式获取属性的“空”状态 - Get the 'Nullable' state programmatically of a Property in Entity Framework 如何检查实体框架类型的属性是否可为空 - How to check if a property of an Entity Framework type is Nullable 实体框架代码首先使列不可为空 - Entity Framework Code first making a column non-nullable 实体框架建立错误的属性关系 - Entity Framework making wrong property relations 对于可以为空的布尔属性,实体框架中的一个或多个实体的验证失败 - Validation failed for one or more entities in Entity Framework for nullable boolean property 如何禁用实体框架来创建Nullable属性 - How to disable entity framework from creating Nullable property's 必需的属性,但通过Code First可以实现Nullable,Entity Framework - Required property but Nullable, Entity Framework through Code First 如何将实体框架4.5设置为永不将任何属性设置为Nullable - How to set entity framework 4.5 to never set any property as Nullable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM