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;
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;
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.
Why is it doing this and how can I resolve it?
Thanks,
Owen
I've managed to work this out using 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. 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.
Here is what I added to my 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.