Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework multiple context and Microsoft Azure. How to update database?

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)
like image 690
chromigo Avatar asked May 28 '14 19:05

chromigo


People also ask

Can we have multiple DbContext in Entity Framework?

Multiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases.

How do I update items in Entity Framework?

This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.

How do you add migration to multiple contexts?

Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.


1 Answers

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
like image 154
chromigo Avatar answered Sep 29 '22 00:09

chromigo