7

I have ASP.NET MVC 5 solution with about 4 projects inside. + EF code-first After reading this article
I tried to separate application EF context(asp.net identity) from my models context.(both contexts in single database)

Now I have 2 project with different contexts.

//main project
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyDB")
    {
    }
}
//another project
public class DALDbContext : DbContext
{
    public DALDbContext()
        : base("MyDB")
    {
    }
    ...
}

I can update database like this:

update-database -ProjectName:myProject.WEB

update-database -ProjectName:myProject.DAL

(in web i have asp.net identity)

If i will publish this solution from VisualStudio in Azure, i can checking "Execute Code First Migrations (runs on application start).".

enter image description here

But if i understand correct, it will be equivalent only one command:

update-database -ProjectName:myProject.WEB

because it's default project.

I don't understand how to run both command. Other side, I tried to execute both commands separately. I wrote something like that in "Package Manager Console":

Update-Database -ConnectionString "Server=tcp:<server_name>.database.windows.net,1433;Database=<database_name>;User ID=<db_user_name>@<server_name>;Password=<password>;Trusted_Connection=False;Encrypt=True;MultipleActiveResultSets=True;Max Pool Size=100;" -ConnectionProviderName "System.Data.SqlClient"

but i get error: "The ConnectionString property has not been initialized."

Or may be try to run one seed method from another...(but i think it is vary bad)

In the end - 2 question:

  1. Is it a good idea to separate one context from another?
  2. How to run this seeds methods on azure?(or your variant)

2 Answers 2

4

I solve the problem myself.

What I did:

Get actual connection string from azure site. It's something like that:

Data Source=tcp:"<address>.database.windows.net,1433;
Initial Catalog=MyDB;
User Id=<login>@<address>;Password=<password>;

also I add "persist security info=True;"(may be it's not nessesary) so we have

Data Source=tcp:"<address>.database.windows.net,1433;
Initial Catalog=MyDB;persist security info=True;
User Id=<login>@<address>;Password=<password>;

(white spaces between words(e.g. "Data Source") is required)

Next paste this connection string into your Web.config file. You must to get something like that: ... .database.windows.net,1433;Initial Catalog=MyDB;persist security info=True;User Id=@;Password=" />

(if you have not these tags - copy paste it and change)

Now looking at your dbContext files.

public class DALDbContext : DbContext
{
    public DALDbContext ()
        : base("MyDB")
    {
    }
...
}

be sure that connectionString name(name="MyDB") from Web.config is equal this value. (and in other dbContext classes)

In the end simply write update-database commands in Package Manager Console:

update-database -ProjectName:myProject.WEB

update-database -ProjectName:myProject.DAL
Sign up to request clarification or add additional context in comments.

Comments

0

I have been encountering the same problem, the following solved it:

  • rename connection ("DefaultConnection") => ("UserConnection"); ("DefaultConnection") => ("DataConnection")
  • update & add connections to web.config
  • restart the publish process a couple of times

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.