2

I would like to specify a non-negative constraint on a column. Not sure how should I go about it. My search on the google didn't help any.

public class Price
{
  [Key, Column(Order = 0)]
  public int     ItemID { get; set; }
  [Key, Column(Order = 1)]
  public int     ItemQty { get; set; }
  [DataType(DataType.Currency), Column(TypeName = "MONEY")]
  public decimal ItemPrice { get; set; }
}

Here I would like ItemQty to be a positive integer. I did think of using Range, but I'm not sure it is the right way...


I went with Range() in the end. Still open to suggestions though.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Van Dame
  • 159
  • 4
  • 13
  • This requires a `CHECK CONSTRAINT` (possible keyword), not just normal DDL table TYPE/NULLABILITY/FK constraints. –  Aug 07 '12 at 02:13
  • Using "check constraint" I found http://stackoverflow.com/questions/9588606/is-it-possible-to-express-a-check-constraint - YMMV. Model first is my choice :) –  Aug 07 '12 at 02:15
  • Actually, for just non-negative, can constrain to to an UNSIGNED server type? (That is, can the server type be specified differently than, but compatible with, the mapped type?) –  Aug 07 '12 at 02:37
  • UNSIGNED has its own implications. See http://stackoverflow.com/questions/10567529/is-it-a-best-practice-to-use-unsigned-data-types-to-enforce-non-negative-and-or – Van Dame Aug 09 '12 at 01:03

1 Answers1

-2

Actually you can't do this there is no such functionality in EF this question has already been answered, you can see it in the link, the second answer gives you the best answer how to do it other way imo and it is a good solution to the problem. Hope it helps you link: answer