locked
How does one create a relationship between an app table and an identity table RRS feed

  • Question

  • User-117172533 posted

    Hi,

    It's a bit hard to decide where to post this question, since it relates to MVC, EF and Identity.

    Imagine a typical web app where there are Users, and then related entities, e.g. Photos. Each user can have multiple photos, so Users have a 1-to-many relationship with Photos.

    I have seen a number of tutorials on EF: Entities are listed as DbSet properties of an App DbContext:

            public class AppContext: DbContext
            {
                public DbSet<Photo> Photos {get; set;}
            }

    The problem is that "Users" are part of the special "ApplicationDbContext" (which inherits from IdentityDbContext). If "Users" were simply my own class I could add it as a DbSet in AppContext. But since "Users" are managed by Identity and are in their own Context, how do I get Users to integrate with the rest of my application, i.e. create a relationship with other entities such as Photos?

    Thanks
    Rael

    Sunday, October 25, 2015 5:02 PM

Answers

  • User-117172533 posted

    I found an answer to my question in this post:

    http://stackoverflow.com/questions/28442094/dbcontext-class-in-asp-net-mvc-5-with-identity-2-0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 27, 2015 9:44 PM

All replies

  • User-986267747 posted

    Hi RaelB,

    The problem is that "Users" are part of the special "ApplicationDbContext" (which inherits from IdentityDbContext). If "Users" were simply my own class I could add it as a DbSet in AppContext. But since "Users" are managed by Identity and are in their own Context, how do I get Users to integrate with the rest of my application, i.e. create a relationship with other entities such as Photos?

    I'm not sure what you mean. In my experience, you'd like to access the "Users" table in your ApplicationDbContext, If the ApplicationDbContext and the UserContext(Identity Context) are access the different databases, we couldn't create a relationship between an app table and an identity table in different databases. Otherwise, you could specify the same table as the user table So that the ApplicationDbContext and the UserContext could operate the datatable.

        public class UsersContext : DbContext
        {
            public UsersContext()
                : base("DefaultConnection")
            {
            }
    
            public DbSet<UserProfile> UserProfiles { get; set; }
        }
    
        [Table("SpecifyTable")]
        public class UserProfile
        {
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int UserId { get; set; }
            public string UserName { get; set; }
        }
    

    About adding custom fields to my MVC 4 registration system, you could refer to the following link.

    http://www.dwdmbi.com/2012/10/adding-custom-fields-to-vs2012-mvc4.html

    Besides, you also could remove the ApplicationDbContext, and you only use the UserContext(Identity Context),  and create some entities are listed as DbSet properties of the UserContext.

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    Sunday, October 25, 2015 11:02 PM
  • User-117172533 posted

    I'm not sure what you mean. In my experience, you'd like to access the "Users" table in your ApplicationDbContext, If the ApplicationDbContext and the UserContext(Identity Context) are access the different databases, we couldn't create a relationship between an app table and an identity table in different databases. Otherwise, you could specify the same table as the user table So that the ApplicationDbContext and the UserContext could operate the datatable.

    Hi,
    I got some ideas, but I did not follow what you meant by UserContext(Identity Context). Your UsersContext inherits from DbContext, so that is a normal EF context, it has nothing to do with Identity??

    -Rael

    Monday, October 26, 2015 7:50 AM
  • User-117172533 posted

    I found an answer to my question in this post:

    http://stackoverflow.com/questions/28442094/dbcontext-class-in-asp-net-mvc-5-with-identity-2-0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 27, 2015 9:44 PM