I am using EF 6 with code first approach. In addition to database constraints check at database level, I would like to provide unique constraints check at POCO level. I have followed article Setting unique Constraint with fluent API? where someone suggested to use [Index()] attribute. I have applied the same in my poco class but looks like it is still throwing an exception from Database level.
Here is my code:
[Key]
public decimal OrderId { get; set; }
[Index("ORDER_CC", 2, IsUnique = true)]
public decimal? MemberId { get; set; }
[Index("ORDER_CC", 3, IsUnique = true)]
public decimal? ItemId { get; set; }
[Index("ORDER_CC", 1, IsUnique = true)]
public decimal? OrderNumber { get; set; }
public decimal? Cost { get; set; }
public DateTime Time { get; set; }
I am not sure what I am doing wrong here?
I also want to ask followings:
- Do I need to keep the same index name as the one I have on my database table for Order?
- How do I know, it is getting validate against EF constraints check or Database constraints check?
- I have kept the same order for index on POCO class compare to one defined in Order table in database.
Here is my Database script:
CREATE UNIQUE INDEX xyz.ORDER_CC ON xyz.Order
(OrderNumber, MemberId, ItemId)
LOGGING
TABLESPACE USERS
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 128K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
Any idea?