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).".
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:
Multiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With