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