实体框架6代码第一种方法 - 唯一约束不起作用 [英] Entity Framework 6 Code First approach - unique constraint doesn't work
问题描述
这是我的代码:我已经在我的poco类中应用了相同的,但是看起来仍然是数据库级别的异常。 / p>
[Key]
public decimal OrderId {get;组; }
[Index(ORDER_CC,2,IsUnique = true)]
public decimal? MemberId {get;组; }
[Index(ORDER_CC,3,IsUnique = true)]
public decimal? ItemId {get;组; }
[Index(ORDER_CC,1,IsUnique = true)]
public decimal? OrderNumber {get;组; }
public decimal?成本{get;组; }
public DateTime Time {get;组;
我不知道我在这里做错什么?
我也想要问:
- 我需要保持相同的索引我的数据库表中有一个名称作为订单?
- 我如何知道它是否通过EF约束检查或数据库约束检查得到验证?
- 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.
$ b
这是我在POCO类中的索引的顺序与数据库中的Order表中定义的索引相同。数据库脚本:
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无限
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
任何想法?
该问题不在Index属性中,它符合您的期望。
索引属性不验证一个唯一的约束,它仅指示实体框架首先在迁移期间在数据库中创建一个索引。从您的问题,您很清楚,您使用EF代码优先,即POCO和编码映射,但您可以使用预先存在的数据库。在这种情况下,应用索引属性是无用的。
这样也可以在数据库中回答你的问题:检查约束。这就是为什么它仍然抛出错误。如果你想在代码中有一个独特的限制,你必须自己写。
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:
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?
The problem is not in the Index attribute, it's in your expectations of what it does.
The Index attribute doesn't validate a unique constraint, it only instructs Entity Framework code-first to create an index in the database during a migration. From your questions it is clear that you use EF code-first, i.e. POCO's and coded mappings, but you work with a preexisting database. In this case, applying index attributes is useless.
So that also answers your question where the constraint is checked: in the database. And that's why it's still throwing errors. If you want a unique constraint in code you'll have to write it yourself.
这篇关于实体框架6代码第一种方法 - 唯一约束不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!