简体   繁体   English

实体框架错误地跨多个列强制执行唯一约束

[英]Entity Framework is incorrectly enforcing a unique constraint across multiple columns

Edit: I'm so sorry I wasted your time. 编辑:我很抱歉我浪费了你的时间。 I missed a line of code when debugging that was causing this problem. 调试导致此问题时,我错过了一行代码。 Someone had put a conditional that was checking if the name already existed in the database and threw this exception if it was. 有人提出了一个条件,即检查数据库中是否已存在该名称,如果存在,则抛出此异常。

I am using Entity Framework code first and I have an entity which contains an index with a unique constraint across two columns as shown in the code below. 我首先使用实体​​框架代码,并且我有一个实体,其中包含一个跨两列的唯一约束的索引,如下面的代码所示。 (I changed the names of properties to generic things so as to not show any of my company's code base) (我将属性的名称更改为通用的东西,以便不显示我公司的任何代码库)

[Table("TB_Table")]
public class Table : IEntity
{
    [Key]
    [Column("TBT_RID")]
    public int Id { get; set; }

    [ForeignKey("Something")]
    [Index("idxSomethingTableName", 1, IsUnique = true)]
    [Column("TBT_SOS_RID")]
    public int SomethingId { get; set; }

    [Index("idxSomethingTableName", 2, IsUnique = true)]
    [MaxLength(255)]
    [Column("TBT_Name")]
    public string Name { get; set; }

    // Navigation properties
    [JsonIgnore]
    public virtual Something Something { get; set; }

    [InverseProperty("Table")]
    [JsonIgnore]
    public virtual ICollection<AssetTag> ObjectTables { get; set; }

}

When inserting a record in SQL, the unique constraint is being enforced properly. 在SQL中插入记录时,正在强制执行唯一约束。 Then, when trying to enter a record through Entity Framework, it tells me that it cannot add a record that has a different "SomethingId" value, but the same "Name" as another record. 然后,当试图通过Entity Framework输入记录时,它告诉我它不能添加具有不同“SomethingId”值的记录,但是与另一个记录具有相同的“Name”。

For example, I can insert these records with SQL: 例如,我可以使用SQL插入这些记录:

insert into TB_Table (TBT_SOS_RID, TGT_Name) values
(1, 'A'),
(1, 'B'),
(30, 'A')

But then I cannot add another (1, 'A') with SQL. 但是我不能用SQL添加另一个(1,'A')。 Okay, great. 好,太棒了。 The constraint is working correctly. 约束正常工作。 Now, if I try to insert a record using entity framework with the values (30, 'B'), I should be able to do this, because the TBT_SOS_RID (SomethingId in C#) is different. 现在,如果我尝试使用具有值(30,'B')的实体框架插入记录,我应该能够这样做,因为TBT_SOS_RID(C#中的SomethingId)是不同的。 Instead, I get an InvalidOperationException with the message "Invalid table, table already Exists". 相反,我得到一个InvalidOperationException,消息“无效表,表已存在”。 This happens on the DbSet.Add() method, before calling the SaveChanges() method. 在调用SaveChanges()方法之前,会在DbSet.Add()方法上发生这种情况。

Can you think of any reason why Entity Framework would think that this is a violation of the unique constraint when SQL does not? 你能想到为什么实体框架会认为这是违反SQL的唯一约束的原因吗?

It appears that Kryptos answered a similar question on how to handle composite indices with foreign keys here: Composite Indices with Foreign Keys 似乎Kryptos在这里回答了关于如何使用外键处理复合索引的类似问题:具有外键的复合指数

I've not tested the solution, but it might help lead you down the correct path, expanding on the answer by niaher in the same question. 我没有对该解决方案进行测试,但它可能有助于引导您走上正确的道路, 在同一问题中扩展答案

You decorated Name as follows: 你装饰名字如下:

[Index("idxSomethingTableName", 2, IsUnique = true)]
[MaxLength(255)]
[Column("TBT_Name")]
public string Name { get; set; }

As far as Entity is concerned, you have stated that Name must be unique, therefore, you cannot enter two names that are the same. 就实体而言,您已声明Name必须是唯一的,因此,您不能输入两个相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM