aneko5
aneko5

Reputation: 101

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 want to 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?

Upvotes: 12

Views: 24282

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

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; } 

Upvotes: 15

Related Questions