2

I am trying to publish my database into azure using EF but am not able to see any tables after migration.

I tried below commands:

Add-Migration MyTables
Update-Database

When I check my Azure DB trough SSMS, I can see logs for all the migration in dbo.__EFMigrationsHistory table, but no table is getting created.

My Context Class:

 public class ApplicationContext : DbContext
{
    public DbSet<Expense> Expenses { get; set; }

    public ApplicationContext(DbContextOptions opts) : base(opts)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    }
}

ConnectionString:

"ConnectionString": { "ExpenseApplicationDB": "Server=tcp:myserver,1432;Initial Catalog=ExpenseDatabase;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" }

Also in publish dialog in database section it showing 'No databases found in the project'.

If I try to achieve same thing using local DB than it's working as expected. Is there anything that am doing wrong?

0

1 Answer 1

0

When you publish with .net EntityFramework, you could click the tab Execute Code First Migration(runs on application start).

However, in EF core, it does not exist it. Additionally, EF does not support Automatic migrations, you may need to manually execute Add-Migration for adding migration files.

Also in publish dialog in database section it showing 'No databases found in the project'.

You also could not exist the database, you could not apply migration on publish.

enter image description here

If you want to apply migration at runtime, you could add the following code int the Configure method of Startup.cs.

 using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
    {
        scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
    }

For more details, you could refer to this thread.

Note, you need to ensure that you could connect to azure database. You could set the connection in azure to the vs local. So that you could also test with azure database in local and debug it.

Sign up to request clarification or add additional context in comments.

9 Comments

"You also could not exist the database, you could not apply migration on publish" - Can you please explain this with some more details, My project type if 'Web API' instead of 'Web Application'.
According to your description, I only could guess it was an asp.net core application.And I get the same problem with you before like no database shows. So I add the code to achieve automatically migration.
You also could add the code into Startup.cs to try.
Tried myDbContext.Database.Migrate(); this in startup.cs, but still migration didn't work.
Actually, you need to add the second code fragment I provided in Configure method in Startup.cs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.