0

I have an ASP.NET Core application. I have structured the application as multiple projects under the solution. In two of the projects I have 2 different contexts for the same database. The problem is I have a table I am using for auditing in both contexts, and this is causing a problem with migration.
My question is:
is there anyway I can make migration ignore creating this table in one of the contexts?

I am getting the error in the following line:

dbContext.Database.Migrate();
Sarahbe
  • 123
  • 1
  • 16
  • I think you should review your system design. because if you ignore the Table for migration, you can't access that table in one of DbContext.you can create a new DbContext only for audit instead of one table in two DbContext. – Morteza Asadi Nov 24 '20 at 03:50
  • you are right, I think it is better to keep it in a separate context. thanks. – Sarahbe Nov 24 '20 at 08:45
  • Dublicate of https://stackoverflow.com/questions/22038924/how-to-exclude-one-table-from-automatic-code-first-migrations-in-the-entity-fram – Alex Yasinovsky Jun 17 '21 at 08:43

2 Answers2

1

in you dbContext you can ignore one or more table using model builder ignore and give the entity class type you want to ignore

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Ignore<YourClassHere>();
}
Ahmed Aljaff
  • 149
  • 2
  • 10
0

You can do this by adding onModelCreating method.

public class ApplicationDbContext : DbContext
{
    public DbSet<TableName> TableNames { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TableName>().ToTable(nameof(TableNames), t => 
        t.ExcludeFromMigrations());
    }
}
Rajon Tanducar
  • 308
  • 4
  • 8