3

I'm trying to make a relationship between the Users from the table generated by Asp.Net Identity with my own table. The relationship must be many to many, since many Users can work on the same Task (which is my table), and same time an User can work on multiple Tasks.

public class Task
{      
    public int ID { get; set; }
    public string Name { get; set; }

    public string UserID { get; set; }

    public virtual ICollection<ApplicationUser> Users { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public int TaskID { get; set; }
    public virtual ICollection<Task> Tasks{ get; set; }

    // rest of the code
}

I try it this way but I get an error during migration (or run time)

"One or more validation errors were detected during model generation:" enter image description here

Please help me solve this problem and archive what I need.

Sampath
  • 63,341
  • 64
  • 307
  • 441
Arianit
  • 543
  • 2
  • 8
  • 24

3 Answers3

4

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:

From MSSQL

and the correct schema:

enter image description here

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 ?

Ermir Beqiraj
  • 867
  • 11
  • 24
1

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

Community
  • 1
  • 1
Sampath
  • 63,341
  • 64
  • 307
  • 441
1

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.

Ryan
  • 609
  • 6
  • 13
  • but is it an acceptable practice to use the applicationdbcontext to work with the business logic? – Arianit Sep 24 '16 at 16:13
  • 1
    Don't forget - it is just **DbContext**. ApplicationDbContext serves as repository itself. Every business logic needs a repository to persist or read data. Simple solutions can use ApplicationDbContext directly, in complex - u create ur own repositories. Also this is a bunch with authentication system. – Ryan Sep 24 '16 at 18:03
  • but what bothers me is that in the future when I need to work with my custom db table, example reference it from an other table or to another table, I have to work with the ApplicationDbContext! This is a thing I kinda don't like. I want to use the ApplicationDbContext just for Identity – Arianit Sep 24 '16 at 23:09