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?

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
Dishant
  • 1,545
  • 12
  • 29

1 Answers1

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.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • "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'. – Dishant May 22 '18 at 10:39
  • 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. – Joey Cai May 23 '18 at 01:34
  • You also could add the code into Startup.cs to try. – Joey Cai May 23 '18 at 01:36
  • Tried myDbContext.Database.Migrate(); this in startup.cs, but still migration didn't work. – Dishant May 24 '18 at 05:16
  • Actually, you need to add the second code fragment I provided in Configure method in Startup.cs – Joey Cai May 24 '18 at 09:18
  • I tried that also, but still migration didn't worked? Any Idea on what might be the issue? – Dishant May 25 '18 at 09:53
  • It's so disturbing. Could you have more detail info like your error message? Also have you go to "Query Editor" on portal to see if any structure have changed? – Joey Cai May 28 '18 at 01:47
  • I am not getting any errors, just the migration code get's executed but table is not getting created. I also checked 'Query Editor' but not changes in that. Would be better If you can share demo working code which I can test by replacing it with my ConnectionString and than compare with my code to find what I am doing wrong. Tnx. – Dishant May 28 '18 at 04:37
  • Finally I got the solution, I deleted my current resource which I was using for DB from Azure and created new resource by following the steps as per [link](https://www.youtube.com/watch?v=dp-DT4iZ5-8) and CF migration worked. Still I am not sure why in publish wizard it's showing 'No database found' and only way to do is via code. – Dishant May 31 '18 at 06:58