Lesdiel Pinzón
Lesdiel Pinzón

Reputation: 35

Foreign key accept null values

How can I make the foreign key of a table "PRODUCT" accept null values?

I require that at the time of filling the PRODUCT form you can save the information without having to select the bank. I generate the form PRODUCT(Controller) with entity framework

The foreign key del model Product -> "public int EntidadID { get; set; }"

I have two related models.

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; }

}

Upvotes: 0

Views: 380

Answers (1)

AndrewM114
AndrewM114

Reputation: 78

Is BankID your foreign key?

If so, in the Product class try changing

public int BankID { get; set; }

to

public int? BankID { get; set; }

and in your database table make sure that the field is nullable.

Upvotes: 6

Related Questions