New Posts New Posts RSS Feed: Code First Migration using DataSourceKey
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Code First Migration using DataSourceKey

 Post Reply Post Reply
Author
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Topic: Code First Migration using DataSourceKey
    Posted: 15-Nov-2012 at 2:43am
Hi,

We are trying to use Code First Migration in our database and it works well but the limitation is the connection string should be in the Web.config.

How can we do this process by invoking the IDataSourceKeyResolver or any way to dynamically compose the connection string to update the database.

Note that I'm doing the Update-Database command via code not in the Package Manager Console.

var migratorConfig = new Model.Migrations.Configuration();
var dbMigrator = new DbMigrator(migratorConfig);
dbMigrator.Configuration.AutomaticMigrationDataLossAllowed = false;
dbMigrator.Update();

Thanks,
Von
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 15-Nov-2012 at 10:58am
Hi Von,
 
Since Migration does not rely on DevForce, (i.e. it's pure EF) IDataSourceKeyResolver does not play a role in it and you will have to use the connectionString in the .config file.
 
   sbelini.
Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Posted: 15-Nov-2012 at 2:57pm
Hi Silvio,

Thanks for the info.
However, do you have an idea if EF has a dynamic way on constructing the connection string on Migration?

I ask this because our BOS' model handles not only one connection to the database and we use IDatasourceKeyResolver to compose the connection string.

Model1 can connection to Database A, Database B, Database C. So how do I perform automatic migration here in the production environment?

Thanks,
Von
Back to Top
sky40627 View Drop Down
Newbie
Newbie
Avatar

Joined: 06-Oct-2009
Posts: 34
Post Options Post Options   Quote sky40627 Quote  Post ReplyReply Direct Link To This Post Posted: 26-Nov-2012 at 10:38pm
Any answers to this question, I am trying to do the same.

Without any luck so far.

I also have a BOS that can connect to different databases.

Thx

Koen
Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Posted: 26-Nov-2012 at 10:50pm
Hi Koen,

We have created a workaround that might help you as well.

On your Custom DbContext:

    public class GlobalDBContext : DbContext
    {
        public GlobalDBContext() :
             base(Web.GetLatestMigrationConnection("MyEntities") ?? "name=MyEntities")
        {

        }
    }

Just add a method call on the base Constructor parameter that compose your connection string on runtime.

Note that when you're creating migration script via Package Manager Console (Add-Migration 'Something') you need to comment out this base(Web.GetLatestMigrationConnection("MyEntities") ?? "name=MyEntities")
and replace it with a basic base("name=MyEntities") that gets connection string from your app.config.
The code that I gave you above only works on production runtime. :)

Thanks,
Von


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down