Entity Framework migrations without access to master db

c# entity-framework entity-framework-6

Question

First of all I'm not a SQL expert....
My assumption is: Don't let any application user access masterdb.
At least that's the default setup I usually run on our shared Azure SaaS SQL host.

I ready various suggestions which tell you to do that:

Database.SetInitializer<AppDataContext>(null);

To my understanding this however disables automatic migrations entirely.
I want automatic (schema) migrations - I only don't want EF to create a new database (or check database existence against masterdb).

What's the proper solution to that?

  • Grant access to the masterdb? (if so can anyone provide a least-privilege snippet)
  • Don't use EF (v6) migrations? :-(

Update 1: I didn't decompile the sources but looked at the mono implementation of MigrateDatabaseToLatestVersion instead.

    /// <inheritdoc />
    public void InitializeDatabase(TContext context)
    {
        Check.NotNull(context, "context");

        var migrator = new DbMigrator(_config);
        migrator.Update();
    }

...and migrator.Update(); will do this... with targetMigration being null.

    public override void Update(string targetMigration)
    {
        base.EnsureDatabaseExists(() => UpdateInternal(targetMigration));
    }

This will basically call UpdateInternal() - which only takes care of migrations. No database existence call, right? I will look into that.

Update 2:

I can see the following call:

IF db_id(N'my-database') IS NOT NULL SELECT 1 ELSE SELECT Count(*) FROM sys.databases WHERE [name]=N'my-database'

But that's fine - no master database access is required for that.

1
1
5/29/2018 11:51:54 AM

Popular Answer

My understanding is that this is a bug in Entity Framework. See the bug report here from MS employee on their GitHub

"... it seems that there is an issue with the database existence check that is done by the DbMigrator—it is attempting to connect to the master database, which fails. We had updated other existence checks to not require this, but it looks like DbMigrator was either missed or there is some reason it couldn’t be done here..."

https://github.com/aspnet/EntityFramework6/issues/522

0
12/16/2018 11:42:59 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow