Many to Many relationship in Asp.Net MVC 5 with Identity table and Custom table

*爱你&永不变心* 提交于 2019-12-01 00:18:23

Try it like this:

  1. public class Projects
    {
        public Projects()
        {
            ApplicationUser = new HashSet<ApplicationUser>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<ApplicationUser> ApplicationUser { get; set;     }
    }
    
  2. Application User



    public class ApplicationUser : IdentityUser
    {
        public ApplicationUser()
        {
            Projects = new HashSet<Projects>();
        }
        public async Task GenerateUserIdentityAsync(UserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }        
        public virtual ICollection <Projects > Projects { get; set; }
    }

  1. Application Context :


    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
        public virtual DbSet<Projects> Projects { get; set; }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

now when I run this Mvc app and register, the db tables I get is like the following:

and the correct schema:

The things to be questioned are a lot, from my point of view important is to determine if you: - can/should you mix application context and your model context ?

Sampath

You can try it as shown below using Fluent API.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Task>()
                .HasMany<ApplicationUser>(s => s.Users)
                .WithMany(c => c.Tasks)
                .Map(cs =>
                        {
                            cs.MapLeftKey("TaskRefId");
                            cs.MapRightKey("ApplicationUserRefId");
                            cs.ToTable("TaskApplicationUser");
                        });

}

Update : you can see this link too.

EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

Error text is not related to your many-to-many relationship. It tips that other built-in entities are not configured properly. So, It would be nice if you provided full definition of your custom DbContext-class and how it is configured.

UPDATE

As i understood u are working with two different contexts. You must work with the same context, cause of u are extending IdentityContext, creating relationships and adding custom types. So problem then will be resolved itself. Hope, this will help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!