Entity Framework migrations without access to master db

c# entity-framework entity-framework-6

Question

First off, I'm not an expert in SQL.
What I'm assuming is: Never allow any application user to access the master database.
That is at least the default configuration I often use on our shared Azure SaaS SQL server.

I found some recommendations that urge you to do it:

Database.SetInitializer<AppDataContext>(null);

To my knowledge, this is fully prevents migrations that are automated..
Only that I don't want EF to generate a new database is that I want automated (schema) migrations (or check database existence against masterdb).

What's the right response to that?

  • Grant masterdb access, please. (If so, could someone share a fragment of least-privilege)
  • Use EF (v6) migrations no more?

1. Update: Rather than decompile the sources, I examined the mono implementation ofMigrateDatabaseToLatestVersion instead.

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

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

This will be accomplished via migrator.Update(); with targetMigration set to null.

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

In essence, this will call UpdateInternal(), which solely handles migrations. No call for database presence, correct? I'll investigate it.

2. Update:

I see the incoming 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 okay; no access to the main database is needed for that.

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

Popular Answer

This, in my opinion, is an Entity Framework problem. On their GitHub page, a Microsoft employee has posted a bug report.

"... it seems that there is a problem with the DbMigrator's database existence check since it tries to connect to the master database but fails. However, it seems that DbMigrator was either overlooked or couldn't be done here despite the fact that we have changed other existence checks to no longer need this."

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