简体   繁体   English

Ef6,多对多,插入和主键

[英]Ef6, Many to many, Inserts and Primary keys

I have a problem with populating the right values in a junction table for a many to many relationship. 对于在多对多关系中的联结表中填充正确的值,我遇到了问题。 In the image below I have simplified what I am trying to do. 在下图中,我简化了我想做的事情。 The "Table on the left" has values in the database that I want to use. “左侧的表”在数据库中具有我要使用的值。 The table of the right is about to receive new records. 右边的表即将接收新记录。 It has a navigation property to the Junction table and the same is true for the table on the left. 它具有“交汇点”表的导航属性,左侧的表也是如此。 The junction table has navigation properties to both tables on its rear and they are set to required . 联结表的背面都有两个表的导航属性,并将它们设置为required

在此处输入图片说明

When I create the new records in the table on the right, I also add to it records in the junction table. 在右侧表中创建新记录时,我还将在联结表中添加到记录中。 The TOL_ID is known as it is saved in the database, but the TOR_ID is about to be created, therefore unknown. 已知TOL_ID是因为它已保存在数据库中,但是TOR_ID将要创建,因此未知。 When I attempt to call the SaveChanges in my context, it tries to save the junction table records first, before the TOR_IDs have been populated for the record on the right. 当我尝试在我的上下文中调用SaveChanges时,它将尝试先保存联结表记录,然后再为右侧的记录填充TOR_ID。 I though that marking the navigation property as Required would make the EF understand that TOR_ID must exist before creating the junction table row. 虽然我将导航属性标记为Required会使EF理解创建联结表行之前TOR_ID必须存在。 Instead it tries to insert an existing TOL_ID and 0, which gives a violation when trying to insert many table on the right records connected to the same TOL_ID. 相反,它尝试插入现有的TOL_ID和0,这在尝试在连接到同一TOL_ID的正确记录上插入许多表时会出现冲突。

Note: Saving the TOR_IDs first and then connecting with junction records is not an option, as the creation of the junction table records are part of a " Slowly changing dimension " type 6 flow. 注意:不能先保存TOR_ID,然后再与联结记录进行连接,因为联结表记录的创建是“ Slowing Changeing Dimensions ”类型6流的一部分。

This is how it really looks like in the code: 这就是代码中的样子:

// The newRating is the new object corresponding the Table on the right
var newRating = new ModuleRating() 
{ 
    // The moduleRating.RatedDriveUnit already exists in the db
    RatedDriveUnit = moduleRating.RatedDriveUnit 
};

newModule.Ratings.Add(newRating);

If you follow the Code First below classes will helpful 如果您遵循Code First,则以下课程将有所帮助

public class TOL
{
    [Key]
    public int TOL_ID { get; set; }
    public int Col1 { get; set; }

    public ICollection<TOR> Tors { get; set; }
}

public class TOR
{
    [Key]
    public int TOR_ID { get; set; }
    public int Col1 { get; set; }

    public ICollection<TOL> Tols { get; set; }
}

public class TolTorContext : DbContext
{
    public DbSet<TOL> Tols { get; set; }
    public DbSet<TOR> Tors { get; set; }
}  

If you follow database first approach,make FK at Join Table and try to chhange id names TOLId , TORId 如果您遵循数据库优先方法,请在Join Table中创建FK并尝试更改ID名称TOLId,TORId

I'am sorry for spending your time. 很抱歉花您的时间。 After some investigation I noticed that the supposed composite unique index (TOL_ID and TOR_ID) of the junction table had been set wrong. 经过一番调查后,我注意到连接表的假定复合唯一索引(TOL_ID和TOR_ID)被设置为错误。 Instead two separate unique indexes had been applied to TOL_ID and TOR_ID, which resulted a constrain violation as soon as one of those two values appeared twice. 而是将两个单独的唯一索引应用于TOL_ID和TOR_ID,一旦这两个值之一出现两次,就会导致约束冲突。

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

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