entity-framework - 可选属性,但需要输入
问题描述
在我的特殊情况下,我正在从旧应用程序中导入数据。在新应用程序中,我有一个GradYear
旧应用程序中不存在的模型属性 ( )。出于历史目的,我需要将数据从旧数据库导入到新数据库。但是,在新应用程序GradYear
中是required
.
使用 EF Core 和 Razor Pages,有没有办法GradYear
在呈现的输入文本框级别设置为必需,但在 db 架构中是可选的?
解决方案
I can think in this possible solution:
- Use a 2 different viewmodels, one for importing from legacy with non required field, other for internal new application use with required attribute, you just need to get sure in the non required viewmodel that you assign a default value when mapping to the DBContext entity.
Another way (not tested) is decorate your property as required but override OnModelCreating like this :
public class YourAppContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
modelBuilder.Entity<YourDbContextEntity>.Property(p => p.GradYear).IsOptional();
...
}
}
推荐阅读
- java - JPA如何从ManyToMany关系中的对象的Arraylist中获取属性
- c# - 如何在 EmguCV 中保存具有透明背景的图像
- node.js - 如何从 Node 的 .env 文件中引用环境变量?
- regex - Regexp_like 在调试器网站上工作,但不在 Oracle SQL Developer 上工作
- javascript - Event.target.value 以不同的格式/意外格式返回值 - Angular 7
- php - 致命错误:未捕获的错误:调用成员函数 bindValue()
- ios - 如何使webview在屏幕上不是全尺寸
- sql-server - 如何通过比较多行和多列来获取 StartDate 和 EndDate
- wordpress - 如何创建分类后下拉简码?
- php - 错误:您在 select 语句中的 SQL 语法有错误