Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new table with EF core

I have an existing database already that was created when i added Identity to the project. Now i wanna add more tables to the database and can't figure out how.

I have created a model for it:

public class Match
{
    public Guid ID { get; set; }
    public string HomeTeam { get; set; }
    public string AwayTeam { get; set; }
    public int FullTimeScore { get; set; }
    public DateTime MatchStart { get; set; }  
    public int PrizePool { get; set; }
}

My context:

public class DynamicBettingUserContext : IdentityDbContext<IdentityUser>
{
    public DynamicBettingUserContext(DbContextOptions<DynamicBettingUserContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

What's the next steps?

like image 368
aneko5 Avatar asked Aug 29 '18 10:08

aneko5


Video Answer


1 Answers

You need to add the Match table in your DynamicBettingUserContext class like below. Then You need to add migration using Add-Migration <YourMigrationName> in Package Manager Console and finally, You have to run Update-Database command in PMC.

public virtual DbSet<Match> Match { get; set; } 
like image 160
vivek nuna Avatar answered Nov 03 '22 01:11

vivek nuna