Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Migrations #1

Open
buldo opened this issue Jun 15, 2016 · 7 comments
Open

EF Migrations #1

buldo opened this issue Jun 15, 2016 · 7 comments

Comments

@buldo
Copy link

buldo commented Jun 15, 2016

Hello.
What is best way for working with EF migrations with this buildpack?

@noliar
Copy link
Owner

noliar commented Jun 15, 2016

You can add some prepublish script into project.json.

eg.
image

:( But it doesn't copy the sqlite database file to the output directory using dotnet publish.

image

image

@buldo
Copy link
Author

buldo commented Jun 15, 2016

Sqlite - not a problem. I'll use PostgreSQL that provided by Heroku :)

@buldo
Copy link
Author

buldo commented Jun 18, 2016

Actually it does not works.
At a prepublish step heroku system environments looks not configured so right ConnectionString cannot be obtained :(

@noliar
Copy link
Owner

noliar commented Jun 19, 2016

I don't know whether the DATABASE_URL value can be used in Npgsql directly or not.
image

I just tested it using hard-coding ConnectionString, and it seems to work.
noliar/ASP.NET-Core-Sample@b59e3a7
https://netcore.herokuapp.com/Account/Register

image

Or setting DefaultConnection in Config Vars.
image

image

image

image

@mattiascibien
Copy link

I tried with this and unfortunately it seems that I cannot attach to the postgresql instance due to ssl being off. And when enabling it te certificate is not accepted.

@buldo
Copy link
Author

buldo commented Jul 14, 2016

@mattiascibien I have an solution for Heroku.
At first add support for Environment Variables in setup method

public Startup(IHostingEnvironment env)
{
    var builder =
                new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                    .AddEnvironmentVariables();

After that you can use this code in ConfigureServices method

public void ConfigureServices(IServiceCollection services)
        {
            string connectionString;
            string herokuDatabaseUrl = Configuration["DATABASE_URL"];
            if (string.IsNullOrWhiteSpace(herokuDatabaseUrl))
            {
                var receiveDataString = Configuration["RECEIVE_DATA"];
                if (string.IsNullOrWhiteSpace(receiveDataString))
                {
                    // We are in local dev environment
                    connectionString = Configuration.GetConnectionString("DefaultConnection");
                }
                else
                {
                    // We are in a process of building and deploying at heroku
                    dynamic receiveData = JsonConvert.DeserializeObject(receiveDataString);
                    connectionString = receiveData.push_metadata.env.DefaultConnection + "SSL Mode=Require;Trust Server Certificate=true;Pooling=false;";
                }
            }
            else
            {
                // We are in production env at Heroku
                connectionString = DbHelpers.DatabaseUrlToPostgreConnectionString(herokuDatabaseUrl);

            }

            services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(connectionString));

Full working example here

@mattiascibien
Copy link

@buldo I actually used the Env var ConnectionStrings:DefaultConnection and it worked like a charm. Now i have a problem with #4 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants