首页 > 解决方案 > 外键接受空值

问题描述

如何使表“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; }

}

标签: c#asp.net-mvcentity-framework

解决方案


BankID 是您的外键吗?

如果是这样,请在 Product 类中尝试更改

public int BankID { get; set; }

public int? BankID { get; set; }

并在您的数据库表中确保该字段可以为空。


推荐阅读