2

I have a fairly basic model for a project developed in Visual Studio 2017 using .Net Core 2.0. Model matches to the following

public class QuoteModel : BaseModel{
   public QuoteModel()
   {
      GetQuoteItems = new List<QuoteItemModel>();
   }

   [Required]
   public int QuoteTypeID { get;set; }

   [Required]
   [ForeignKey("QuoteTypeID")]
   public QuoteTypeModel QuoteType { get;set;}

   public ICollection<QuoteItemModel> GetQuoteItems { get; set; }
}

I've tried both with and without the [Required] annotation and still run into this issue. The issue is that if I create a new Quote, the QuoteTypeID defaults to 0 so if the user doesn't select a type, the model still remains valid. I'd prefer if the model invalidated itself by not defaulting the foreign key to a value, however I cannot figure out why foreign keys are defaulting to a value at all as much of my research has shown that it normally defaults to null requiring that an ID be provided.

How can I get the foreign key 'QuoteTypeID' to not have a default value but require a value be set?

Also, for the sake of clarity, BaseModel simply provides a few standard fields I wanted in a majority of my models like 'CreatedOn' and 'CreatedBy'.

J-Americano
  • 188
  • 1
  • 10
  • add an explicit setter, that sets the Id when you set the type – johnny 5 Oct 17 '17 at 14:25
  • I'm not fully sure I follow. I cannot explicitly set a type if no types exist. My aim is to have quotes fail if no quote types have been setup. However currently, it successfully sets it to '0' which is considered a valid model, which results in an exception being thrown as the SaveChanges() call will fail to insert this into the table. – J-Americano Oct 17 '17 at 16:49
  • What are you talking about? If no types exist your key should be nullable. If they type exists when you set the type in the setter also set the id – johnny 5 Oct 17 '17 at 17:23
  • I have a foreign key that is defaulting to zero when a new object is created. This default zero value means it passes model validation resulting in an SQL exception being thrown due to the foreign key constraint not being met. It defaults to zero if no type is selected, or if no types exist meaning I can't handle the in an informative way without manual validation. I'd prefer if the foreign key not being set invalidated the model rather than causes an exception. Allowing the key to be nullable wouldn't do this. Setting the key if a type exists already works. – J-Americano Oct 17 '17 at 18:53
  • >" I'd prefer if the foreign key not being set invalidated the model rather than causes an exception" If that is that case, you can do it in 2 ways, create a generic base validator that applies a data annotation to every field with a foreign-key attribute. Or create a Generic base validator which does the equivalent before save – johnny 5 Oct 17 '17 at 19:25

1 Answers1

2

[ForeignKey("QuoteTypeID")]

Should be placed above the foreign key itself and should have the model name in the quotes i.e.,

[ForeignKey("QuoteType")]
[Required]
public int QuoteTypeID { get;set; }
ZerosAndOnes
  • 1,083
  • 1
  • 13
  • 25
  • Apologies, I copied this exact as it is now, however both [Required] and [ForeignKey] have been moved and tried multiple different ways with no change. It still defaults to 0 and allows it to be saved to the database this way. – J-Americano Oct 17 '17 at 14:02
  • In that case, EF data migration mightn't be detecting the change as it is a small one. Please have a look at Chris Pratt's answer on https://stackoverflow.com/questions/28547951/update-existing-database-with-entity-framework-code-first-in-mvc it might help. – ZerosAndOnes Oct 17 '17 at 14:07