How to add tables from models to ApplicationDbContext

kobosh 176 Reputation points
2021-11-02T22:12:38.337+00:00

Hi I created .netcore version 5 web app with authentication (user individual account). Now I want to add more tables to this automatically created ApplicationDbContext from models ( Lesson and Instructor). I tried code shown below and ran update-database command but this did add lesson table or instructor table although the command ran without error
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{

        }
        public DbSet<Lesson> Lesson { get; set; }
        public DbSet<Instructor> Instructor { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Lesson>().ToTable("Lesson");
            modelBuilder.Entity<Instructor>().ToTable("Instructor");

        }
    }
Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2021-11-03T07:06:23.93+00:00

    Hi @kobosh ,

    Do you mean the Lesson and Instructor table is not created in the database? If that is the case, you could refer the following steps:

    1. Create a Asp.net core 5 with authentication (user individual account)
    2. Create Lesson class and Instructor class:
      public class Instructor  
      {  
          public int Id { get; set; }  
          public string Name { get; set; }  
      }  
      public class Lesson  
      {  
          public int Id { get; set; }  
          public string LessonName { get; set; }  
      }  
      
    3. Modify the ApplicationDbContext as below:
      public class ApplicationDbContext : IdentityDbContext  
      {  
          public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
              : base(options)  
          {  
          }   
          public DbSet<Lesson> Lesson { get; set; }  
          public DbSet<Instructor> Instructor { get; set; }  
          protected override void OnModelCreating(ModelBuilder modelBuilder)  
          {  
              base.OnModelCreating(modelBuilder);  
      
              modelBuilder.Entity<Lesson>().ToTable("Lesson");  
              modelBuilder.Entity<Instructor>().ToTable("Instructor");  
          }  
      
      }
    4. In the Package Manager Console, run the follow migration command: add-migration addlesson
      update-database
    5. After running the above command success, the table has already created in the database: 146083-image.png

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    2 people found this answer helpful.

  2. Anonymous
    2021-11-09T06:04:57.69+00:00

    Hi @kobosh ,

    SqlException: Cannot insert duplicate key row in object 'dbo.AspNetUsers' with unique index 'UserNameIndex'. The duplicate key value is (HABNOUF@Stuff .COM).

    From the exception message, there already has the username with "HABNOUF@Stuff .COM".

    By default, when using Asp.net core Identity, the AspNetUser has the unique index 'UserNameIndex', it will prevent to register the user with the same username and show the above exception:

    147594-image.png

    To solve this exception, you can create a new account with a different username.

    Or you can refer the following methods to remove the unique index and allow the duplicate username:

    1. Try to delete the UserNameIndex unique index via SQL Server Management Studio (SSMS)
    2. Try to override the IdentityUser class and disable the unique index for UserName.
      Refer Add, download, and delete custom user data to Identity in an ASP.NET Core project and Identity model customization in ASP.NET Core.
    3. After executing the add-migration command, go to the migration file, find the UserNameIndex unique index definition and remove it. Then, execute the update-database command.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.