简体   繁体   中英

How to create more than one table from one database (ASP.NET MVC)?

I am working with ASP.NET MVC. The plan is to create a Website where you can switch between to tables.

The website works with one table but the problem is that when I switch to the other table I can not see the table.

I use one database for the program and created a second public class for the second table in my Context.cs.

For the first table I used:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ImpEx>()
                .HasIndex(b => b.Ex_Nr)
                .IsUnique();
}

But for the second I am not allowed to use a second method of it.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ImpExQ30>()
                .HasIndex(d => d.Ex_Nr)
                .IsUnique();
}

The error is that I am not allowed to use a second method because I have one in my DB already

You cannot have two OnModelCreating methods in one context.

But you can have two or many more builder statements:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ImpEx>()
            .HasIndex(b => b.Ex_Nr)
            .IsUnique();

        modelBuilder.Entity<ImpExQ30>()
            .HasIndex(d => d.Ex_Nr)
            .IsUnique();

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM