简体   繁体   English

必需的属性,但通过Code First可以实现Nullable,Entity Framework

[英]Required property but Nullable, Entity Framework through Code First

How to make a property required (for field validation) but Nullable for the DB code migrations? 如何为数据库代码迁移创建所需的属性(用于字段验证)但是Nullable?

I do have a DB table with a thousand entries. 我有一个包含一千个条目的数据库表。 recently was required to add a required DateTime property. 最近需要添加一个必需的DateTime属性。

    [Required]
    [Display(Name = "Birth", Order = 10)]
    public DateTime? Birth { get; set; } 

If I set [Required] annotation, the Code first migration will add NOT NULL to the column declaration. 如果我设置[Required]注释,则代码首次迁移将向列声明添加NOT NULL。 However all current entries does not have "Birth" data. 但是,所有当前条目都没有“出生”数据。 and it will be NULL. 它将为NULL。

the Birth property should be required to the view field validatoin, but it could be nullable into the DB. 视图字段validatoin应该要求Birth属性,但它可以在文件中可以为空。 Is that possible somehow? 这有可能吗?

I already tried add "?" 我已经尝试添加“?” (nullable) to the property and "virtual" without success. (可空)到财产和“虚拟”没有成功。

Use your model for DB/Entity communication. 使用您的模型进行数据库/实体通信。

Use a View Model for your UI layer. 使用视图模型作为UI层。 Mark Required on the property in the ViewModel, mark Nullable on the model. 在ViewModel中的属性上标记为必需,在模型上标记Nullable。 Perform casting in your code as needed. 根据需要在代码中执行转换。 Move all the UI-related attribute decoration (like Display, validation/etc) to the ViewModel as well. 将所有与UI相关的属性修饰(如显示,验证/等)也移动到ViewModel。

Casting can automatically be performed with the AutoMapper plugin, available via the NuGet package manager. 使用AutoMapper插件可以自动执行强制转换可以通过NuGet包管理器获得。

One quick way could be overriding the OnModelCreating() method of your DB Context. 一种快速方法可能是覆盖数据库上下文的OnModelCreating()方法。

This way : 这条路 :

public class AppContext : DbContext
{
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
      // ...
      modelBuilder.Entity<YourModelEntity>.Property(p => p.Birth).IsOptional();
   }

}

or another proper way is that you can create an extended class of the generic EntityTypeConfiguration type for your model and then add this specific configuration to the DBModelBuilder in the OnModelCreating() method this way : 或者另一种正确的方法是,您可以为模型创建通用EntityTypeConfiguration类型的扩展类,然后以这种方式将此特定配置添加到OnModelCreating()方法中的DBModelBuilder:

public class YourModelTypeConfiguration : EntityTypeConfiguration<YourModelType>
{
    public YourModelTypeConfiguration()
    {
        // ... some other configurations ;

        Property(p => p.Birth).IsOptional();
    }
}

Note that you need the 请注意,您需要

using System.Data.Entity.ModelConfiguration; 

at top of your class file. 在您的类文件的顶部。

Then in the OnModelCreating() method you should add this : 然后在OnModelCreating()方法中你应该添加:

    public class AppContext : DbContext
{
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
      // quick and dirty solution
      // modelBuilder.Entity<YourModelEntity>.Property(p => p.Birth).IsOptional()

      // cleaner solution
      modelBuilder.Configurations.Add(new YourModelTypeConfiguration());
   }

}

This way you keep your specific configurations separated and don't mix everything together. 这样,您可以将特定配置分开,并且不要将所有内容混合在一起。

When applying code first migration the "Birth" database field should be nullable. 在应用代码首次迁移时,“Birth”数据库字段应该可以为空。

I hope this helps. 我希望这有帮助。

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

相关问题 实体框架代码第一个可选属性映射为必需 - Entity Framework Code First Optional Property is mapped as Required 首先 Enitify Framework 代码:使用所需的相关导航属性更新实体 - Enitify Framework code first: Updating Entity With required related navigational property 通过关系表的实体框架代码优先导航属性 - Entity Framework Code First navigation property through relational table 实体框架代码首先使列不可为空 - Entity Framework Code first making a column non-nullable 实体框架代码首先添加 DbSet 属性 - Entity Framework code first adding DbSet property 实体框架不可为空的列映射到可为空的实体属性 - Entity Framework Non-nullable column is mapped to a nullable entity property 使用实体框架(代码优先)将具有可空关联的实体更新/更改为null - Using Entity Framework (Code First) to update/change an entity with a nullable association to null 实体框架6代码优先-必需的Enum数据类型不起作用 - Entity Framework 6 Code First - Required Enum data type not working 首先是实体框架代码中所有属性的必需规则 - Required rule for all properties in entity framework code first 实体框架中不可为空的导航属性为null - Non nullable navigation property is null in Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM