locked
Adding Tables to ASP.NET Identity RRS feed

  • Question

  • User-305496339 posted

    Adding Tables to ASP.NET Identity

    I have added the following DBSets to the ApplicationDBContext Class following the instructions in this link https://stackoverflow.com/questions/28448368/mvc-code-first-how-to-add-more-tables-to-the-same-db-context. However, I get a message telling me that I am missing a using directive or an assembly reference. 

    Can you provide your expertise to help me,
    thank you

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("DefaultConnection", throwIfV1Schema: false)
            {
            }
            public DbSet<tblMainMenu> tblMainMenus { get; set; }
            public DbSet<tblSubMenu> tblSubMenus { get; set; }
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }

    Wednesday, May 8, 2019 3:59 PM

Answers

  • User1120430333 posted

    Maybe, the compiler doesn't know where tblSubMenu class or the other one are located, which you would use a 'using' statment pointing to the namespace the classes are in, or maybe, you need to put the classes in the correct namespace.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 8, 2019 7:54 PM
  • User-2054057000 posted

    Thanks for your help. But I couldn't find the classes for the other tables that are used with the Identity system. Would i just put the classes in the ApplicationDbContext Class?

    Your are using Identity along with some of your custom work that includes separate classes. In this case you will have to create 2 DbContext classes, one for Identity and one for the other database. Important - By this I mean you will have 2 databases for your application.

    The same thing is done in this CMS hosted on GitHub that uses Identity along with a separate database. You can download it and check the codes which will help you to go in the right direction.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2019 3:47 PM
  • User1520731567 posted

    Hi rkrex,

    I was wondering what if I wanted to add a table(s) to the Identity System?

    For example,I want to add a table named testModels:

      public class testModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
    
        }
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("DefaultConnection", throwIfV1Schema: false)
            {
            }
    
            public DbSet<testModel> testModels { get; set; }
    
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }

    and then you need to execute the following commands in the Package Manager Console in Visual Studio:

    1.Enable-Migrations

    2.Add-Migration testModel

    3.Update-Database

    Like the picture:

    More details,you could refer to:

    https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 10, 2019 5:35 AM

All replies

  • User1120430333 posted

    Maybe, the compiler doesn't know where tblSubMenu class or the other one are located, which you would use a 'using' statment pointing to the namespace the classes are in, or maybe, you need to put the classes in the correct namespace.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 8, 2019 7:54 PM
  • User-305496339 posted

    Thanks for your help. But I couldn't find the classes for the other tables that are used with the Identity system. Would i just put the classes in the ApplicationDbContext Class?

    Wednesday, May 8, 2019 10:59 PM
  • User-2054057000 posted

    Thanks for your help. But I couldn't find the classes for the other tables that are used with the Identity system. Would i just put the classes in the ApplicationDbContext Class?

    Your are using Identity along with some of your custom work that includes separate classes. In this case you will have to create 2 DbContext classes, one for Identity and one for the other database. Important - By this I mean you will have 2 databases for your application.

    The same thing is done in this CMS hosted on GitHub that uses Identity along with a separate database. You can download it and check the codes which will help you to go in the right direction.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2019 3:47 PM
  • User-305496339 posted

    Thank You So Kindly for your Help. I was wondering what if I wanted to add a table(s) to the Identity System? Thank You !!!

    Thursday, May 9, 2019 6:06 PM
  • User1520731567 posted

    Hi rkrex,

    I was wondering what if I wanted to add a table(s) to the Identity System?

    For example,I want to add a table named testModels:

      public class testModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
    
        }
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("DefaultConnection", throwIfV1Schema: false)
            {
            }
    
            public DbSet<testModel> testModels { get; set; }
    
    
            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }
        }

    and then you need to execute the following commands in the Package Manager Console in Visual Studio:

    1.Enable-Migrations

    2.Add-Migration testModel

    3.Update-Database

    Like the picture:

    More details,you could refer to:

    https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 10, 2019 5:35 AM