【问题标题】:Foreign key accept null values外键接受空值
【发布时间】:2019-11-13 05:04:36
【问题描述】:

如何使表“PRODUCT”的外键接受空值?

我要求您在填写 PRODUCT 表单时可以保存信息,而无需选择银行。我使用 实体框架

生成表单 PRODUCT(Controller)

外键del模型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-mvc entity-framework


    【解决方案1】:

    BankID 是您的外键吗?

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

    public int BankID { get; set; }

    public int? BankID { get; set; }

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

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多