简体   繁体   English

多对多关系删除第三次添加ICollection时的连接表

[英]Many-To-Many relationship deleting join table on third addition of ICollection

I'm having an issue with my site. 我的网站有问题。 I have a UserProfile class with two navigation properties at the bottom of my file as follows; 我有一个UserProfile类,在我的文件底部有两个导航属性,如下所示;

public virtual ICollection<UserProfile> Followers { get; set; }
public virtual ICollection<UserProfile> Following { get; set; }

This then creates another table (which I believe is called the intersect table?) in which it manages this relationship by itself. 然后创建另一个表(我相信称为交叉表?),它自己管理这种关系。 This works fine and I can add/remove from this List. 这工作正常,我可以在此列表中添加/删除。 The part i'm having trouble with is when I want to add another navigation property called "BlockedUsers", so it's like this; 我遇到麻烦的部分是当我想添加另一个名为“BlockedUsers”的导航属性时,就像这样;

public virtual ICollection<UserProfile> Followers { get; set; }
public virtual ICollection<UserProfile> Following { get; set; }
public virtual ICollection<UserProfile> BlockedUsers { get; set; }

When I run Add-Migration and Update-Database, it deletes the table it created previously for my Following/Followers List and breaks my sites feature therefore I'm unable to add/remove from the Following/Followers List. 当我运行Add-Migration和Update-Database时,它会删除之前为我的关注/关注者列表创建的表并破坏我的网站功能,因此我无法在关注/关注者列表中添加/删除。

Why is it doing this and how can I resolve it? 为什么这样做,我该如何解决?

Thanks, 谢谢,

Owen 欧文

I've managed to work this out using FluentAPI. 我已经设法使用FluentAPI解决了这个问题。 I've not really looked into it before but I realised it was the only way to achieve what I was trying to do. 我之前没有真正研究过它,但我意识到这是实现我想要做的事情的唯一方法。

If you take the first block of navigation properties (Following/Follower) this will create an intersect table. 如果您获取第一个导航属性块(Follow / Follower),这将创建一个交叉表。 However, if I add the third navigation property to do the same thing, it'll delete this table. 但是,如果我添加第三个导航属性来执行相同的操作,它将删除此表。

In order to have both tables for BlockedUsers and Following/Followers, I had to use FluentAPI to create this intersect manually. 为了同时拥有BlockedUsers和Follow / Followers两个表,我必须使用FluentAPI手动创建此交叉。

Here is what I added to my IdentityModel.cs - 这是我添加到IdentityModel.cs的内容 -

protected override void OnModelCreating( DbModelBuilder modelBuilder)
    {
        // Sets up Many-To-Many relationship for Following/Followers UserProfiles
        modelBuilder.Entity<UserProfile>()
        .HasMany( t => t.Followers )
        .WithMany( t => t.Following );

        // Sets up Many-To-Many relationship for Blocked UserProfiles
        modelBuilder.Entity<UserProfile>()
        .HasMany( t => t.BlockedUsers )
        .WithMany();

        base.OnModelCreating( modelBuilder );
    }

I hope this helps someone with the same issue. 我希望这可以帮助有同样问题的人。

I am however still wondering why it deleted the table in the first place before I added any FluentAPI code. 然而,我仍然想知道为什么在我添加任何FluentAPI代码之前它首先删除了表。

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

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