MVC5, EF 6.0 Code First, SQL
I have a remotely hosted MVC5 website using an SQL database. My objective is to easily code up to copy or copy/delete certain records out of the hosted database to a local database. So I added an additional context to my development project as shown.
Public Class WebDbContext
Inherits DbContext
Public Sub New()
MyBase.New("name=WebDbConnection")
Me.Configuration.LazyLoadingEnabled = True
End Sub
Public Shared Function Create() As WebDbContext
Return New WebDbContext()
End Function
Public Property table1 As DbSet(Of Class1)
.
.
Public Property tableN As DbSet(Of ClassN)
End Class
and in web.config I added the connection string
<configuration>
.
.
<connectionStrings>
<add name="WebDbConnection" connectionString="Data Source=<ip>;Initial Catalog=<dbname>;User ID=<id>;Password=<pwd>;" providerName="System.Data.SqlClient" />
</connectionStrings>
.
.
This new context allows me to access and manipulate the website database from the development project. I will clearly need another context for the database I want to save locally, but I'm just getting started with this. I started in this direction because the Class definitions for the new context will always be up to date with the hosted database since the development project originates the Class definitions that are in the hosted database. Is there a better way to code this access to the remote database?
But my real question is: What might I expect with regard to this new WebDbContext when my next add-migration happens? Does each context create an EF add-migration requirement, or is it just the Class definitions that create the add-migration requirement and the additional context(s) won't be a problem?
Regarding access of the production database from the development project, I recognize we treat production databases differently, but this project is in early development and a quick programmatic solution is being sought. I understand at a later time when production is underway a different solution may be necessary.
Or, of course as often is the case, if there's a completely different and generally accepted way to accomplish my objective as stated at the outset, what might that be?
Not too sure if I'm fully grasping your question, but if I get the gist of your question, your trying to have 2x databases (1 localdb + 1 SQL server hosted) and want to know how to setup data migrations.
First you will need 2x connectionStrings, 1 for local and 1 for SQL
Then in your package manager console you will need to run the commands:
enable-migrations
add-migration -Name firstDbMigrationScript -ConnectionStringName firstConnnectionString
add-migration -Name secondDbMigrationScript -ConnectionStringName secondConnnectionString
Sorry if this stuff doesn't work strait up, I'm coding from memory