Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在第三次添加ICollection时删除联接表的多对多关系_C#_Asp.net_Entity Framework - Fatal编程技术网

C# 在第三次添加ICollection时删除联接表的多对多关系

C# 在第三次添加ICollection时删除联接表的多对多关系,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我的网站有问题。我有一个UserProfile类,在我的文件底部有两个导航属性,如下所示 public virtual ICollection<UserProfile> Followers { get; set; } public virtual ICollection<UserProfile> Following { get; set; } 公共虚拟ICollection Followers{get;set;} 在{get;set;}之后的公共虚拟ICollectio

我的网站有问题。我有一个UserProfile类,在我的文件底部有两个导航属性,如下所示

public virtual ICollection<UserProfile> Followers { get; set; }
public virtual ICollection<UserProfile> Following { get; set; }
公共虚拟ICollection Followers{get;set;}
在{get;set;}之后的公共虚拟ICollection
然后,它会创建另一个表(我认为称之为intersect表?),在该表中它自己管理这个关系。这很好,我可以添加/删除此列表。我遇到的问题是,当我想添加另一个名为“BlockedUsers”的导航属性时,它是这样的

public virtual ICollection<UserProfile> Followers { get; set; }
public virtual ICollection<UserProfile> Following { get; set; }
public virtual ICollection<UserProfile> BlockedUsers { get; set; }
公共虚拟ICollection Followers{get;set;}
在{get;set;}之后的公共虚拟ICollection
公共虚拟ICollection BlockedUsers{get;set;}
当我运行“添加迁移和更新数据库”时,它会删除以前为我的追随者列表创建的表,并破坏我的网站功能,因此我无法从追随者列表中添加/删除

它为什么这样做?我如何解决它

谢谢


欧文

我用FluentAPI解决了这个问题。我以前并没有认真研究过,但我意识到这是实现我想要做的事情的唯一途径

如果使用第一块导航属性(跟随/跟随),将创建一个相交表。但是,如果我添加第三个导航属性来执行相同的操作,它将删除此表

为了同时拥有BlockedUsers和Follow/Followers的表,我必须使用FluentAPI手动创建这个intersect

以下是我添加到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 );
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//为以下/关注者用户配置文件设置多对多关系
modelBuilder.Entity()
.HasMany(t=>t.追随者)
.有许多(t=>t.跟随);
//为被阻止的用户配置文件设置多对多关系
modelBuilder.Entity()
.HasMany(t=>t.BlockedUsers)
.有许多();
基于模型创建(modelBuilder);
}
我希望这能帮助有同样问题的人


然而,我仍然想知道为什么在我添加任何FluentAPI代码之前,它首先删除了表。

我已经设法使用FluentAPI解决了这个问题。我以前并没有认真研究过,但我意识到这是实现我想要做的事情的唯一途径

如果使用第一块导航属性(跟随/跟随),将创建一个相交表。但是,如果我添加第三个导航属性来执行相同的操作,它将删除此表

为了同时拥有BlockedUsers和Follow/Followers的表,我必须使用FluentAPI手动创建这个intersect

以下是我添加到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 );
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//为以下/关注者用户配置文件设置多对多关系
modelBuilder.Entity()
.HasMany(t=>t.追随者)
.有许多(t=>t.跟随);
//为被阻止的用户配置文件设置多对多关系
modelBuilder.Entity()
.HasMany(t=>t.BlockedUsers)
.有许多();
基于模型创建(modelBuilder);
}
我希望这能帮助有同样问题的人


但是,我仍然想知道为什么在我添加任何FluentAPI代码之前,它首先删除了表。

您是先使用编码的吗?如果是,请向我们展示您的实体映射!是的,我首先使用代码,但是我没有添加任何自定义实体映射。我只是在我的模型中设置了所有内容。您是先使用编码吗?如果是,请向我们展示您的实体映射!是的,我首先使用代码,但是我没有添加任何自定义实体映射。我只是在我的模型中设置了所有内容。