Home > Blockchain >  create a new sqlite table within the same database that was created during initialmigration
create a new sqlite table within the same database that was created during initialmigration

Time:12-20

I am new to fairly new to programming and started a new project using asp.net core and MVC. Since i wanted to see what was going on behind the scenes, i implemented authentication and authorisation manually using IdentityUser and IdentityDbContxt. During the initial migration, a bunch of tables were created that allows the users to register and then login in. I have implemented all of that. Now I want to create a new table within the same database that will let the users submit their names and address. I believe i can just create a new table using sqlite GUI and work with that, but how do I create a UserProfileModel.cs file and have that schema show up on the database? My apologies if my question is not very clear. Also , I am working with microsoft stack on a mac :). Thanks.

CodePudding user response:

First of all, you can always extend the IdientityUser, add migration, update the database and names and addresses will be added to the table.

public class UserProfileModel:IdentityUser

If you want to have more models with tables, you have to make a class that extends the IdentityDbContext, pass the db options to the constructor and add your DbDets, wich will be use for creating the tables.

 public class AppDbContext : IdentityDbContext<User>
{
    public DbSet<UserProfileModel> Residences { get; set; }

    public RealEstateDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

Then, where you configure the services you have to add the database,

services.AddDbContext<AppDbContext>(options => options.UseSqlite(configuration.GetConnectionString("Your Connection String")));

I assume you have already made that, maybe with IdentityDbContext, but you can add your custom DbContext.

Now you can inject your AppDbContext wherever you want to use it. Probably to a service or a repository.

  • Related