1

I have a question about creating a new table in ASP.NET Core MVC application. I've created it with "Individual User Accounts" so I have Identity tables in DB.

How can I create my own tables in this DB? Should I create a new DB Context?

For example: I want to create a tables for students and questions(and their answers).

4
  • Use a different DbContext for your own Tables/Classes. Commented Apr 16, 2018 at 12:04
  • @Nikolaus Alright, could you help me with this?Where in ApplicationDbContext.cs should I add a new context? Will be very grateful for any links about this. Commented Apr 16, 2018 at 12:08
  • @Алексей you may need to follow this tutorial : learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/… Commented Apr 16, 2018 at 12:47
  • As for the comments/answers of others, where is you exact Problem you need to define Classes, used in your DbContext, ... What do you have (Code)? Commented Apr 16, 2018 at 13:08

2 Answers 2

4

Since you already said you used the Individual User Accounts Identity template, you can just add your custom models on it, something like:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //....
        }
    public DbSet<Student> Students{ get; set; }
    public DbSet<Question> Questions { get; set; }
    public DbSet<Answer> Answers { get; set; }
    //....
    }

To add/run migrations after the new changes -- From the Tools Menu locate Package Console Manager under Nuget Console Manager. Select the project with your ApplicationDbContext and run the commands as shown in the screenshot.

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

So, I added those classes and their DbSet . I update database and all seems fine. Where I can find created tables? In SQL Server Obj.Manager I only have Identity tables.
Did you create/run migrations?
Nope, could you give some info about this?
Wow, m8, thanks for your attention and time.You have saved a huge amount of my time. P.S. I've tried to use "Add migration" command without '-' so it doesn't work.
@FelixToo im trying to move from ef6 to efcore but there seems to be a disconnect for me... are there any gd article/links to where to setup organised fluent builder code and what not... ef6 code first seemed better in this regard. Asking as i get the above and hoping you spent more time in this and than me, so perhaps you can show/tell me what works..
2
  1. Create Your Models:

    suppose you have the following model:

    public class Post{
    
      public int Id {get; set;}
      public string Name {get; set;}
    
    }
    
  2. in the root directory, create a folder called Persistence (or you can call it whatever you want)

  3. Inside Persistence folder add a class like this

    public class MyDbContext : DbContext {
    
        public DbSet<Post> Posts {get; set;}
        public MyDbContext((DbContextOptions<MyDbContext> options)
         : base (options)
        {
    
        }
    }
    
  4. Now in the Startup.cs, add this line to the ConfigureServices function:

    services.AddDbContext<MyDbContext>(options => options.UseSqlServer("connStr"));

  5. Now the Time to create your first migration, by using dotnet cli

dotnet ef migrations add AddPostsTable --context MyDbContext

Find More At: docs here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.