c# - 外键接受空值
问题描述
如何使表“PRODUCT”的外键接受空值?
我要求在填写PRODUCT表格时,您可以保存信息而无需选择银行。我使用实体框架生成表单 PRODUCT(Controller)
外键 del model Product -> "public int EntidadID { get; set; }"
我有两个相关的模型。
public class Product
{
[Key]
public int ProductID { get; set; }
public string NumContrato { get; set; }
public float TasaReal { get; set; }
public decimal Capital { get; set; }
public DateTime FechaValor { get; set; }
public DateTime FechaVencimiento { get; set; }
public int Plazo { get; set; }
public int BankID { get; set; }
public virtual CATbank CATbank { get; set; }
}
public class CATbank
{
[Key]
public int BankID { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
解决方案
BankID 是您的外键吗?
如果是这样,请在 Product 类中尝试更改
public int BankID { get; set; }
至
public int? BankID { get; set; }
并在您的数据库表中确保该字段可以为空。
推荐阅读
- sql - SQL Server CTE 层次结构数据类型转换
- angular - 如何在 Angular 中翻译库而不在应用程序中初始化翻译服务?
- c++ - 无效密码导致无限显示循环
- batch-file - 如何从批处理文件运行多个命令?
- javascript - JS - 我可以导入具有多个文件的同名导出吗?
- c++ - 是否可以使用 std::pair 作为 std::set 的键而每个元素没有任何重复?
- raku - 实例化 Raku 类,并在构造函数中更新实例变量
- c++ - c++ 多个虚拟嵌套函数
- android - 在`.symlinks/plugins/geolocator_web/ios`中找不到`geolocator_web`的podspec
- python - 如何在python中对来自JsonResponse的数据进行分组?